您的位置:首页 > 产品设计 > UI/UE

HDU 3157 Crazy Circuits

2015-10-06 22:37 465 查看

Crazy Circuits

Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3157
64-bit integer IO format: %I64d Java class name: Main

You’ve just built a circuit board for your new robot, and now you need to power it. Your robot circuit consists of a number of electrical components that each require a certain amount of current to operate. Every component has a + and a - lead, which are connected on the circuit board at junctions. Current flows through the component from + to - (but note that a component does not "use up" the current: everything that comes in through the + end goes out the - end).

The junctions on the board are labeled 1, ..., N, except for two special junctions labeled + and - where the power supply terminals are connected. The + terminal only connects + leads, and the - terminal only connects - leads. All current that enters a junction from the - leads of connected components exits through connected + leads, but you are able to control how much current flows to each connected + lead at every junction (though methods for doing so are beyond the scope of this problem1). Moreover, you know you have assembled the circuit in such a way that there are no feedback loops (components chained in a manner that allows current to flow in a loop).

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 100;
struct arc {
int to,flow,next;
arc(int x = 0,int y = 0,int z = -1) {
to = x;
flow = y;
next = z;
}
} e[maxn*maxn];
int head[maxn],cur[maxn],d[maxn],du[maxn],tot;
void add(int u,int v,int flow) {
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,0,head[v]);
head[v] = tot++;
}
bool bfs(int S,int T) {
queue<int>q;
q.push(S);
memset(d,-1,sizeof d);
d[S] = 1;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].flow && d[e[i].to] == -1) {
d[e[i].to] = d[u] + 1;
q.push(e[i].to);
}
}
}
return d[T] > -1;
}
int dfs(int u,int T,int low) {
if(u == T) return low;
int a,tmp = 0;
for(int &i = cur[u]; ~i; i = e[i].next) {
if(e[i].flow && d[e[i].to] == d[u]+1&&(a=dfs(e[i].to,T,min(e[i].flow,low)))) {
e[i].flow -= a;
e[i^1].flow += a;
low -= a;
tmp += a;
if(!low) break;
}
}
if(!tmp) d[u] = -1;
return tmp;
}
int Dinic(int S,int T,int ret = 0) {
while(bfs(S,T)) {
memcpy(cur,head,sizeof head);
ret += dfs(S,T,INF);
}
return ret;
}
int main() {
char a[10],b[10];
int n,m,u,v,bound,S,T,SS,TT;
while(scanf("%d%d",&n,&m),n||m) {
memset(head,-1,sizeof head);
memset(du,0,sizeof du);
int sum = S = 0;
T = n + 1;
SS = T + 1;
TT = SS + 1;
for(int i = tot = 0; i < m; ++i) {
scanf("%s%s%d",a,b,&bound);
if(a[0] == '+') u = S;
else sscanf(a,"%d",&u);
if(b[0] == '-') v = T;
else sscanf(b,"%d",&v);
add(u,v,INF);
du[u] -= bound;
du[v] += bound;
}
for(int i = 0; i <= T; ++i) {
if(du[i] > 0) {
add(SS,i,du[i]);
sum += du[i];
} else add(i,TT,-du[i]);
}
u = Dinic(SS,TT);
add(T,S,INF);
v = Dinic(SS,TT);
if(u + v == sum) printf("%d\n",e[tot-1].flow);
else puts("impossible");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: