zoj 2532 Internship 找出关键割边(当增加其容量后使最大流增大) 最小割+dfs

CIA headquarter collects data from across the country through its classified network. They have been using optical fibres long before it's been deployed on any civilian projects. However they are still under a lot pressure recently because the data are growing rapidly. As a result they are considering upgrading the network with new technologies that provide a few times wider bandwidth. In the experiemental stage, they would like to upgrade one segment of their original network in order to see how it performs. And as a CIA intern it's your responsibility to investigate which segment could actually help increase the total bandwidth the headquarter receives, suppose that all the cities have infinite data to send and the routing algorithm is optimized. As they have prepared the data for you in a few minutes, you are told that they need the result immediately. Well, practically immediately.

Input

Input contains multiple test cases. First line of each test case contains three integers n, m and l, they represent the number of cities, the number of relay stations and the number of segments. Cities will be referred to as integers from 1 to n, while relay stations use integers from n+1 to n+m. You can saves assume that n + m <= 100, l <= 1000 (all of them are positive). The headquarter is identified by the integer 0.

The next l lines hold a segment on each line in the form of a b c, where a is the source node and b is the target node, while c is its bandwidth. They are all integers where a and b are valid identifiers (from 0 to n+m). c is positive. For some reason the data links are all directional.

The input is terminated by a test case with n = 0. You can safely assume that your calculation can be housed within 32-bit integers.

Output

For each test print the segment id's that meets the criteria. The result is printed in a single line and sorted in ascending order, with a single space as the separator. If none of the segment meets the criteria, just print an empty line. The segment id is 1 based not 0 based.

Sample Input
2 1 3
1 3 2
3 0 1
2 0 1
2 1 3
1 3 1
2 3 1
3 0 2
0 0 0
Sample Output
2 3
<hey here is an invisible empty line>

//

解题思路:

题目意思概括下就说能否找到(关键割边):当增加其容量后使最大流增大。

关键割边(u->v)在残留网络中要满足的条件:

(1):满流

(2):源点能到u,v能到汇点

 

算法:

(1):用sap跑一边最大流

(2):在残留网络中分别以源点和汇点为起始点dfs

(3):遍历所有的边判断每一条边是否符合关键割边条件



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=21010;
const int M=250000;
const int inf=(1<<28);
int head[N];
struct Edge
{
    int u,v,next,w,id;
} edge[M];
int cnt,n,s,t;//n从0开始  0->n-1
void addedge(int u,int v,int w,int id)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].id=id;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].u=v;
    edge[cnt].v=u;
    edge[cnt].id=id;
    edge[cnt].w=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int sap()
{
    int pre[N],cur[N],dis[N],gap[N];
    int flow=0,aug=inf,u;
    bool flag;
    for(int i=0; i<n; i++)
    {
        cur[i]=head[i];
        gap[i]=dis[i]=0;
    }
    gap[s]=n;
    u=pre[s]=s;
    while(dis[s]<n)
    {
        flag=0;
        for(int &j=cur[u]; j!=-1; j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].w>0&&dis[u]==dis[v]+1)
            {
                flag=1;
                if(edge[j].w<aug) aug=edge[j].w;
                pre[v]=u;
                u=v;
                if(u==t)
                {
                    flow+=aug;
                    while(u!=s)
                    {
                        u=pre[u];
                        edge[cur[u]].w-=aug;
                        edge[cur[u]^1].w+=aug;
                    }
                    aug=inf;
                }
                break;
            }
        }
        if(flag) continue;
        int mindis=n;
        for(int j=head[u]; j!=-1; j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].w>0&&dis[v]<mindis)
            {
                mindis=dis[v];
                cur[u]=j;
            }
        }
        if((--gap[dis[u]])==0)
            break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return flow;
}


//初始化  cnt=0;memset(head,-1,sizeof(head));
int vis1[N],vis2[N];
void dfs1(int u)
{
int i,v;
vis1[u]=true;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(edge[i].w>0&&!vis1[v])
dfs1(v);
}
}
void dfs2(int u)
{
int i,v;
vis2[u]=true;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(edge[i^1].w>0&&!vis2[v])
dfs2(v);
}
}
 int main()
 {
     int cn,m,l;
     while(scanf("%d%d%d",&cn,&m,&l)==3&&cn)
     {
         cnt=0;
         memset(head,-1,sizeof(head));
         n=cn+m+2;
         t=0,s=n-1;//important
         for(int i=1;i<=l;i++)
         {
             int u,v,w;scanf("%d%d%d",&u,&v,&w);
             addedge(u,v,w,i);
         }
         for(int i=1;i<=cn;i++) addedge(s,i,inf,-1);
         int tmp=sap();
         memset(vis1,0,sizeof(vis1));
         memset(vis2,0,sizeof(vis2));
         dfs1(s);
         dfs2(t);
         int ans[N];
         int ln=0;
         for(int i=0;i<cnt;i+=2)
         {
             int u=edge[i].u,v=edge[i].v;
             if(!edge[i].w&&vis1[u]&&vis2[v])
                 ans[ln++]=edge[i].id;
         }
         if(!ln)
             printf("\n");
         else
         {
             for(int i=0;i<ln-1;i++)
                 printf("%d ",ans[i]);
             printf("%d\n",ans[ln-1]);
         }
     }
 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值