zoj 2532 Internship(最大流求割边)

Internship

Time Limit: 5 Seconds       Memory Limit: 32768 KB

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>

题意就是 在网络中找出一条边(一次只能找一条边) 修改他的容量  使得整个网络的流量得到扩展

从源点S到n个发射点连一条容量为无穷大的边,再连接L条光纤,容量为该光纤的带宽,以0为汇点T。题目转化成扩大哪些边的容量可以增加最大流。先在图上求最大流,在残量网络中,把S能到的点标记为1,把能到T的点标记为2,那么当一条边的出发点为1、结束点为2,这条边就是关键割边,也就是所求的边之一。如果有一条边的其中一个点未被标记,那么扩大这条边的容量,也就不过是从源点到达的点多了一个,或者到达汇点的点多了一个,但是在这个残量网络中依旧没有从S到T的增广路。


因为有n个发射点 添加一个超级源点s s到发射点的容量为INF

另外 一开始做此题时  都是用结构体写的。。一直TLE  后来修改为都用数组 才过了。。

时间差这么多么。。。

<span style="font-family:KaiTi_GB2312;font-size:18px;">//#include <cstdio>
//#include <iostream>
//#include <cstring>
//#include <cmath>
//#include <algorithm>
//#include <string.h>
//#include <string>
//#include <vector>
//#include <queue>
//
//#define MEM(a,x) memset(a,x,sizeof a)
//#define eps 1e-8
//#define MOD 10009
//#define INF 0x3fff3fff
//#define ll __int64
//#define bug cout<<"here"<<endl
//#define fread freopen("ceshi.txt","r",stdin)
//#define fwrite freopen("out.txt","w",stdout)
//
//using namespace std;
//
//void read(int &x)
//{
//    char ch;
//    x=0;
//    while(ch=getchar(),ch!=' '&&ch!='\n')
//    {
//        x=x*10+ch-'0';
//    }
//}
//
//const int MAXN=110;
//const int MAXM=3010;
//struct  SAP
//{
//    int to[MAXM],next[MAXM],flow[MAXM];
//    int head[MAXN],dis[MAXN],pre[MAXN],cur[MAXN],gap[MAXN];
//    int n,st,ed,tot;
//
//    void init()
//    {
//        MEM(head,-1);
//        tot=2;
//    }
//    void addedge(int u,int v,int c)
//    {
//        to[tot]=v;flow[tot]=c;next[tot]=head[u];
//        head[u]=tot++;
//        to[tot]=u;flow[tot]=0;next[tot]=head[v];
//        head[v]=tot++;
//    }
//
//    void bfs()
//    {
//        MEM(dis,0x3f);
//        queue<int> que; que.push(ed);
//        dis[ed]=0;
//        while(!que.empty())
//        {
//            int u=que.front(); que.pop();
//            ++gap[dis[u]];
//            for(int i=head[u];i!=-1;i=next[i])
//            {
//                int &v=to[i];
//                if(flow[i^1]&&dis[v]>n)
//                {
//                    dis[v]=dis[u]+1;
//                    que.push(v);
//                }
//            }
//        }
//    }
//
//    int Maxflow(int ss,int tt,int nn)
//    {
//        st=ss; ed=tt; n=nn;
//        int ans=0,MIN=INF;
//        int u;
//        for(int i=0;i<=n;i++)
//        {
//            cur[i]=head[i];
//            gap[i]=0;
//        }
//        u=pre[st]=st;
//        bfs();
//        while(dis[st]<n)
//        {
//            bool flag=0;
//            for(int &p=cur[u];p!=-1;p=next[p])
//            {
//                int &v=to[p];
//                if(flow[p]&&dis[u]==dis[v]+1)
//                {
//                    flag=1;
//                    MIN=min(MIN,flow[p]);
//                    pre[v]=u;
//                    u=v;
//                    if(u==ed)
//                    {
//                        ans+=MIN;
//                        while(u!=st)
//                        {
//                            u=pre[u];
//                            flow[cur[u]]-=MIN;
//                            flow[cur[u]^1]+=MIN;
//                        }
//                        MIN=INF;
//                    }
//                    break;
//                }
//            }
//            if(flag)  continue;
//            int minDis=n-1;
//            for(int p=head[u];p!=-1;p=next[p])
//            {
//                int &v=to[p];
//                if(flow[p]&&minDis>dis[v])
//                {
//                    minDis=dis[v];
//                    cur[u]=p;
//                }
//            }
//            if(--gap[dis[u]]==0)  break;
//            gap[dis[u]=minDis+1]++;
//            u=pre[u];
//        }
//        return ans;
//    }
//
//    int mark[MAXN];
//    void cut()
//    {
//        MEM(mark,0);
//        queue<int> que;
//        que.push(st); mark[st]=1;
//        while(!que.empty())
//        {
//            int u= que.front(); que.pop();
//            for(int p=head[u];p!=-1;p=next[p])
//            {
//                int &v=to[p];
//                if(flow[p]&&!mark[v])
//                {
//                    mark[v]=1;
//                    que.push(v);
//                }
//            }
//        }
//        que.push(ed); mark[ed]=2;
//        while(!que.empty())
//        {
//            int u= que.front(); que.pop();
//            for(int p=head[u];p!=-1;p=next[p])
//            {
//                int &v=to[p];
//                if(flow[p^1]&&!mark[v])
//                {
//                    mark[v]=2;
//                    que.push(v);
//                }
//            }
//        }
//    }
//}sap;
//
//int from[MAXM],to[MAXM];
//
//int main()
//{
    fread;
//    int n,m,l;
//    while(scanf("%d%d%d",&n,&m,&l)!=EOF)
//    {
//        if(n==0)  break;
//        int s=n+m+1,t=0;
//        sap.init();
//        for(int i=1;i<=n;i++)
//            sap.addedge(s,i,INF);
//        for(int i=1;i<=l;i++)
//        {
//            int c;
//            scanf("%d%d%d",&from[i],&to[i],&c);
//            sap.addedge(from[i],to[i],c);
//        }
//        sap.Maxflow(s,t,s);
//        sap.cut();
//        bool flag=0;
//        for(int i=1;i<=l;i++)
//        {
//            int &u=from[i],&v=to[i];
//            if(sap.mark[u]==1&&sap.mark[v]==2)
//            {
//                if(flag)  printf(" ");
//                printf("%d",i);
//                flag=1;
//            }
//        }
//        puts("");
//    }
//    return 0;
//}

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

void read(int &x)
{
    char ch;
    x=0;
    while(ch=getchar(),ch!=' '&&ch!='\n')
    {
        x=x*10+ch-'0';
    }
}

const int MAXN=110;
const int MAXM=3010;
int to[MAXM],next[MAXM],cap[MAXM],flow[MAXM];
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];

void init()
{
    tol=0;
    MEM(head,-1);
}
//加边  单向图三个参数 双向图四个参数
void addedge(int u,int v,int w,int rw=0)
{
    to[tol]=v; cap[tol]=w; next[tol]=head[u];
    flow[tol]=0; head[u]=tol++;
    to[tol]=u; cap[tol]=rw; next[tol]=head[v];
    flow[tol]=0; head[v]=tol++;
}
//输入参数:起点 终点 点的总数
//点的编号没有影响 只要输入点的总数
int sap(int start,int end,int N)
{
    MEM(gap,0);
    MEM(dep,0);
    memcpy(cur,head,sizeof(head));
    int u=start;
    pre[u]=-1;
    gap[start]=N;
    int ans=0;
    while(dep[start]<N)
    {
        if(u==end)
        {
            int Min=INF;
            for(int i=pre[u];i!=-1;i=pre[to[i^1]])
                if(Min>cap[i]-flow[i])
                    Min=cap[i]-flow[i];
            for(int i=pre[u];i!=-1;i=pre[to[i^1]])
            {
                flow[i]+=Min;
                flow[i^1]-=Min;
            }
            u=start;
            ans+=Min;
            continue;
        }
        bool flag=0;
        int v;
        for(int i=cur[u];i!=-1;i=next[i])
        {
            v=to[i];
            if(cap[i]-flow[i]&&dep[v]+1==dep[u])
            {
                flag=1;
                cur[u]=pre[v]=i;
                break;
            }
        }
        if(flag)
        {
            u=v;
            continue;
        }
        int Min=N;
        for(int i=head[u];i!=-1;i=next[i])
            if(cap[i]-flow[i]&&dep[to[i]]<Min)
            {
                Min=dep[to[i]];
                cur[u]=i;
            }
        gap[dep[u]]--;
        if(!gap[dep[u]])  return ans;
        dep[u]=Min+1;
        gap[dep[u]]++;
        if(u!=start)  u=to[pre[u]^1];
    }
    return ans;
}

int vis[MAXN];
int from[MAXM],tto[MAXM];

void dfs1(int s)
{
    queue<int> que;
    que.push(s); vis[s]=1;
    while(!que.empty())
    {
        int u=que.front(); que.pop();
        for(int i=head[u];i!=-1;i=next[i])
        {
            int &v=to[i];
            if((cap[i]-flow[i])&&!vis[v])
            {
                vis[v]=1;
                que.push(v);
            }
        }
    }
}

void dfs2(int t)
{
    queue<int> que;
    que.push(t); vis[t]=2;
    while(!que.empty())
    {
        int u=que.front(); que.pop();
        for(int i=head[u];i!=-1;i=next[i])
        {
            int &v=to[i];
            if((flow[i^1]-cap[i^1])&&!vis[v])
            {
                vis[v]=2;
                que.push(v);
            }
        }
    }
}

int main()
{
//    fread;
    int n,m,l;
    while(scanf("%d%d%d",&n,&m,&l)!=EOF)
    {
        if(n==0)  break;
        init();
        int s=n+m+1,t=0;
        for(int i=1;i<=n;i++)  addedge(s,i,INF);
//        while(l--)
//        {
            int u,v,w;
//            scanf("%d%d%d",&u,&v,&w);
//            addedge(u,v,w);
//        }
        for(int i=1;i<=l;i++)
        {
            int c;
            scanf("%d%d%d",&from[i],&tto[i],&c);
            addedge(from[i],tto[i],c);
        }
        sap(s,t,s+1);
        MEM(vis,0);
        dfs1(s);
        dfs2(t);
        int flag=0;
        for(int i=1;i<=l;i++)
        {
            int &u=from[i],&v=tto[i];
            if(vis[u]==1&&vis[v]==2)
            {
                if(flag)  printf(" ");
                printf("%d",i);
                flag=1;
            }

        }
        puts("");
    }
    return 0;
}
</span>

加注释和不加注释的两端代码都可以AC


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值