QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#504897 | #9106. Zayin and Dirichlet | untitledtwo# | RE | 0ms | 0kb | C++14 | 2.3kb | 2024-08-04 17:04:43 | 2024-08-04 17:04:44 |
answer
#include<bits/stdc++.h>
using namespace std;
int a[30], b[30], c[30];
bool isb[100010];
struct Edge
{
int to, nxt, flow, val;
} e[100010];
int ecnt = 0, head[1010];
inline void addedge(int from, int to, int flow, int val, bool ib = false)
{
e[ecnt].to = to;
e[ecnt].flow = flow;
e[ecnt].val = val;
e[ecnt].nxt = head[from];
isb[ecnt] = ib;
head[from] = ecnt++;
}
inline void connect(int from, int to, int flow, int val)
{
addedge(from, to, flow, val);
addedge(to, from, 0, -val);
}
int n, m, s, t, pre[1010], lst[1010], dis[1010];
bool vis[1010];
const int INF = INT_MAX >> 1;
queue<int> q;
inline int spfa()
{
for(int i = 0; i <= t; i++)
dis[i] = INF, vis[i] = false;
q.push(s);
vis[s] = true, dis[s] = 0, pre[t] = -1;
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -1; i = e[i].nxt)
{
int v = e[i].to;
if(e[i].flow > 0 && dis[v] > dis[u] + e[i].val)
{
dis[v] = dis[u] + e[i].val;
pre[v] = u, lst[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
return pre[t];
}
inline void Flow(int &maxflow, int &mincost)
{
maxflow = mincost = 0;
while(spfa() > 0)
{
int u = t;
mincost += dis[t];
maxflow++;
while(u != s)
{
int v = pre[u], i = lst[u];
e[i].flow--, e[i ^ 1].flow++;
if(isb[i])
{
e[i].val = 999 - e[i].val;
}
u = v;
}
}
mincost = (mincost + 999) / 1000;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &n, &m);
s = 0, t = n + 3 * m + 1;
ecnt = 0;
for(int i = 0; i <= t; i++)
head[i] = -1;
for(int i = 1; i <= n; i++)
connect(s, i, 1, 0);
int ans = 0;
for(int i = 1; i <= m; i++)
{
scanf("%d %d %d", &a[i], &b[i], &c[i]);
ans += b[i] + c[i];
int k;
scanf("%d", &k);
while(k--)
{
int u;
scanf("%d", &u);
connect(u, i + n, 1, 0);
connect(u, i + n + m, 1, 0);
connect(u, i + n + m + m, 1, 0);
}
connect(i + n, t, a[i], 0);
addedge(i + n + m, t, 2 * b[i], 999, true), addedge(t, i + n + m, 0, 0, true);
addedge(i + n + m + m, t, c[i], 1000), addedge(t, i + n + m + m, c[i], 0);
}
int maxflow, mincost;
Flow(maxflow, mincost);
if(maxflow < n)
printf("-1\n");
else
printf("%d\n", ans - mincost);
}
return 0;
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
2 2 1 1 1 1