题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1532
题意描述: 有你个城市和m个中转站,有一个数据接收站(编号为0),城市从1---n编号,中转站从n+1---m编号,数据从每个城市发出最后到接收站接受,现在问提高哪些边中一条的容量使得接收站接收的数据增加
分析:网络流的基础题,该题就是一个简单的找割边,不解释!
总结:最近发现是该时候提升一下编码能力啦,该题虽然简单但是我依旧wa了很多次,原因是dfs1中的dfs1写成了dfs,尼玛查了半天的错,唉,你就是个弱智啊!!!
代码:
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=0x3fffffff;
const int N = 200;
const int E = 5000;
int vis[N],flag[N];
int e,head[N];
int dep[N],que[N],cur[N];
struct node
{
int x,y;
int nxt;
int c;
}edge[E];
void addedge(int u ,int v,int c)
{
edge[e].x=u;
edge[e].y=v;
edge[e].nxt=head[u];
edge[e].c=c;
head[u]=e++;
edge[e].x=v;
edge[e].y=u;
edge[e].nxt=head[v];
edge[e].c=0;
head[v]=e++;
}
int maxflow(int s,int t)
{
int i,j,k,front,rear,top, min,res=0;
while(1)
{
memset(dep,-1,sizeof(dep));
front=0;
rear=0;
que[rear++]=s;
dep[s]=0;
while(front!=rear)
{
i=que[front++];
for(j=head[i];j!=-1;j=edge[j].nxt)
if(edge[j].c&&dep[edge[j].y]==-1)
{
dep[edge[j].y]=dep[i]+1;
que[rear++]=edge[j].y;
}
}
if(dep[t]==-1)
break;
memcpy(cur,head,sizeof(head));
for(i=s,top=0;;)
{
if(i==t)
{
min=inf;
for(k=0;k<top;k++)
if(min>edge[que[k]].c)
{
min=edge[que[k]].c;
front=k;
}
for(k=0;k<top;k++)
{
edge[que[k]].c-=min;
edge[que[k]^1].c+=min;
}
res+=min;
i=edge[que[top=front]].x;
}
for(j=cur[i];cur[i]!=-1;j=cur[i]=edge[cur[i]].nxt)
if(dep[edge[j].y]==dep[i]+1&&edge[j].c)
break;
if(cur[i]!=-1)
{
que[top++]=cur[i];
i=edge[cur[i]].y;
}
else
{
if(top==0)
break;
dep[i]=-1;
i=edge[que[--top]].x;
}
}
}
return res;
}
void dfs(int u)
{
vis[u]=1;
for(int i=head[u];i!=-1;i=edge[i].nxt)
{
int y = edge[i].y;
if(vis[y]==0&&edge[i].c)
dfs(y);
}
}
void dfs1(int u)
{
flag[u] = 1;
for(int i=head[u];i!=-1;i=edge[i].nxt)
{
int y = edge[i].y;
if(edge[i^1].c&&flag[y]==0)
dfs1(y);
}
}
int main()
{
int n, m, l;
int i, x,y;
int ans[E];
int c;
while(scanf("%d%d%d",&n,&m,&l)!=EOF)
{
if(n==0)break;
e = 0;
memset(head,-1,sizeof(head));
for(i=0;i<l;i++)
{
scanf("%d%d%d",&x,&y,&c);
addedge(x,y,c);
}
for(i=1;i<=n;i++)
addedge(n+m+1,i,inf);
maxflow(n+m+1,0);
memset(vis,0,sizeof(vis));
memset(flag,0,sizeof(flag));
dfs(n+m+1);
dfs1(0);
int k=0;
for(i=0;i<e;i+=2)
{
x = edge[i].x;
y = edge[i].y;
if(vis[x]==1 && flag[y]==1&&edge[i].c==0)
ans[k++]=i/2+1;
}
if(k==0)
printf("\n");
else
{
printf("%d",ans[0]);
for(i=1;i<k;i++)
printf(" %d",ans[i]);
printf("\n");
}
}
return 0;
}