HDU 5294 Tricks Device(最短路+最小割)

Tricks Device

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4038    Accepted Submission(s): 1148


Problem Description
Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the entrance of the tomb while Dumb Zhang’s at the end of it. The tomb is made up of many chambers, the total number is N. And there are M channels connecting the chambers. Innocent Wu wants to catch up Dumb Zhang to find out the answers of some questions, however, it’s Dumb Zhang’s intention to keep Innocent Wu in the dark, to do which he has to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that’s the only chance Innocent Wu can catch Dumb Zhang.
Unfortunately, Dumb Zhang masters the art of becoming invisible(奇门遁甲) and tricks devices of this tomb, he can cut off the connections between chambers by using them. Dumb Zhang wanders how many channels at least he has to cut to stop Innocent Wu. And Innocent Wu wants to know after how many channels at most Dumb Zhang cut off Innocent Wu still has the chance to catch Dumb Zhang.
 

Input
There are multiple test cases. Please process till EOF.
For each case,the first line must includes two integers, N(<=2000), M(<=60000). N is the total number of the chambers, M is the total number of the channels.
In the following M lines, every line must includes three numbers, and use ai、bi、li as channel i connecting chamber ai and bi(1<=ai,bi<=n), it costs li(0<li<=100) minute to pass channel i.
The entrance of the tomb is at the chamber one, the end of tomb is at the chamber N.
 

Output
Output two numbers to stand for the answers of Dumb Zhang and Innocent Wu’s questions.
 

Sample Input
 
  
8 9 1 2 2 2 3 2 2 4 1 3 5 3 4 5 4 5 8 1 1 6 2 6 7 5 7 8 1
 

Sample Output
 
  
2 6

题意:给你一个无向有权图,起点在1终点在n,人物只走最短路中的边,求最少切掉几条边人物不能从1走到n和最多切多少条边人物依然能从1走到n。

思路:先求一遍最短路,然后把最短路上的边边权置为1重新建图,再求一遍最短路,m-dis[n]就是第二个问题的答案。跑一遍网络流,结果就是第一个问题的答案。这个题在网络流专题里有类似的题目。

代码:

#include<bits/stdc++.h>
#define ll long long
#define maxn 600010
#define inf 0x3f3f3f3f
using namespace std;
struct edge{
    edge(){};
    edge(int from,int to,int val,int flow):from(from),to(to),val(val),flow(flow){}
    int from,to,val,flow;
};
int n,m;
vector<edge>p;
vector<int>num[maxn];
int dis[maxn],vis[maxn];
int cur[maxn],zuo[maxn],you[maxn],w[maxn];
void add(int x,int y,int z){
    p.push_back(edge(x,y,z,0));
    p.push_back(edge(y,x,0,0));
    int xx=p.size();
    num[x].push_back(xx-2);
    num[y].push_back(xx-1);
}
int SPFA(int s,int t)
{
    for(int i=0;i<=n;i++)dis[i]=inf;
    memset(vis,0,sizeof(vis));
    queue<int>qq;
    qq.push(s);
    dis[s]=0;
    vis[s]=1;
    while (!qq.empty()){
        int u=qq.front();
        qq.pop();
        vis[u]=0;
        for (int i=0;i<num[u].size();i++){
            edge e=p[num[u][i]];
            if (e.val&&dis[e.to]>dis[u]+e.val){
                dis[e.to]=dis[u]+e.val;
                if(!vis[e.to])
                {
                    qq.push(e.to);
                    vis[e.to]=1;
                }
            }
        }
    }
    return dis[t];
}
bool bfs(int s,int t){
    int i,j;
    memset (dis,-1,sizeof(dis));
    queue<int>qq;
    qq.push(s);
    dis[s]=0;
    while (!qq.empty()){
        int u=qq.front();
        qq.pop();
        for (i=0;i<num[u].size();i++){
            edge& e=p[num[u][i]];
            if (dis[e.to]==-1&&e.val>e.flow){
                dis[e.to]=dis[u]+1;
                qq.push(e.to);
            }
        }
    }
    return dis[t]!=-1;
}
int dfs(int x,int t,int val){
    if (x==t||val==0){
        return val;
    }
    int flow=0,f;
    for (int& i=cur[x];i<num[x].size();i++){
        edge& e=p[num[x][i]];
        if (dis[x]+1==dis[e.to]&&(f=dfs(e.to,t,min(val,e.val-e.flow)))>0){
            e.flow+=f;
            p[num[x][i]^1].flow-=f;
            flow+=f;
            val-=f;
            if (val==0)break;
        }
    }
    return flow;
}
int dinic(int s,int t){
    int flow=0;
    while (bfs(s,t)){
        memset (cur,0,sizeof(cur));
        flow+=dfs(s,t,inf);
    }
    return flow;
}
void init()
{
    p.clear();
    for (int i=0;i<maxn;i++){num[i].clear();}
}
int main(){
      while(scanf("%d%d",&n,&m)!=EOF){
          init();
          int s=1,t=n;
            for(int i=0; i<m; i++)
            {
                scanf("%d %d %d",&zuo[i],&you[i],&w[i]);
                add(zuo[i],you[i],w[i]);
                add(you[i],zuo[i],w[i]);
                zuo[m+i]=you[i];you[m+i]=zuo[i];
                w[m+i]=w[i];
            }
            int jud=SPFA(s,t);
            if(jud==inf){puts("0 0");continue;}
            init();
            for(int i=0; i<2*m; i++)
            if(dis[zuo[i]]+w[i]==dis[you[i]])
            {
                    add(zuo[i],you[i],1);
            }
            jud=SPFA(s,t);
            int aa=dinic(s,t);
            printf("%d %d\n",aa,m-jud);
      }
      return 0;
}


猜你喜欢

转载自blog.csdn.net/lsd20164388/article/details/81064812
今日推荐