比赛 |
20120704 |
评测结果 |
WWWWTTTTTT |
题目名称 |
危险游戏 |
最终得分 |
0 |
用户昵称 |
CC |
运行时间 |
0.000 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2012-07-04 11:45:50 |
显示代码纯文本
#include <cstdio>
#include <algorithm>
#include <cstring>
typedef long long ll;
const ll INF = 100000000000,end = 10040;
struct edge {
ll y,v,c;
edge *next,*opt;
edge (ll y,ll v,ll c,edge *next): y(y),v(v),c(c),next(next) {};
}*a[10050],*pre[10050];
ll n,T,Q,ans;
ll d[100050][3],dis[10050];
void create(ll x,ll y,ll v,ll c) {
a[x] = new edge(y,v,c,a[x]);
a[y] = new edge(x,-v,0,a[y]);
a[x]->opt = a[y];a[y]->opt = a[x];
}
bool spfa() {
ll q[100000] = {0},head = 0,tail = 0;
bool inq[10050] = {0};
for (int i = 0;i <= end;i++) dis[i] = INF;
dis[0] = 0;
while (head <= tail) {
int now = q[head++];
for (edge *p = a[now];p;p = p->next)
if (p->c > 0 && dis[p->y] > dis[now] + p->v) {
dis[p->y] = dis[now] + p->v;
pre[p->y] = p;
if (!inq[p->y]) inq[q[++tail] = p->y] = 1;
}
inq[now] = 0;
}
return dis[end] != INF;
}
ll flow() {
ll flow = 0,t;
while (spfa()) {
t = INF;
for (int i = end;i != 0;i = pre[i]->opt->y)
t = std::min(t,pre[i]->c);
for (int i = end;i != 0;i = pre[i]->opt->y) {
pre[i]->c -= t;pre[i]->opt->c += t;
}
flow += dis[end] * t;
}
return flow;
}
int main() {
freopen("tubea.in","r",stdin);
freopen("tubea.out","w",stdout);
scanf("%lld%lld", &n, &T);
ll p,q;
for (int i = 1;i <= T;i++) {
scanf("%lld%lld%lld", &d[i][0], &d[i][1], &d[i][2]);
}
for (int i = 1;i <= T;i++) {
create(d[i][1],d[i][0],d[i][2],INF);
create(d[i][0],d[i][1],d[i][2],INF);
}
for (int i = 1;i <= n;i++)
create(i,end,0,1);
create(0,1,0,n);
ans = flow();
printf("%lld\n", ans);
scanf("%lld", &Q);
for (int i = 1;i <= Q;i++) {
scanf("%lld%lld", &p, &q);
memset(a,0,sizeof(a));
memset(pre,0,sizeof(pre));
d[p][2] = q;
for (int i = 1;i <= T;i++) {
create(d[i][1],d[i][0],d[i][2],INF);
create(d[i][0],d[i][1],d[i][2],INF);
}
for (int i = 1;i <= n;i++)
create(i,end,0,1);
create(0,1,0,n);
ans = flow();
printf("%lld\n", ans);
}
return 0;
}