zoj 2760 How Many Shortest Path 最大流

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760

Given a weighted directed graph, we define the shortest path as the path who has the smallest length among all the path connecting the source vertex to the target vertex. And if two path is said to be non-overlapping, it means that the two path has no common edge. So, given a weighted directed graph, a source vertex and a target vertex, we are interested in how many non-overlapping shortest path could we find out at most.

题目描述:求一个有向图起点到终点的边不相交的最短路径的条数。

算法分析:floyd+最大流。针对网络流算法而建的模型中,s-t对应于实际中每一种方案,所以此题中的s-t就对应于题目中的一条源点到汇点的最短路径,最大流就是最短路径条数。

接下来就是怎么建模的问题:既然s-t对应于一条最短路径,那么s-t路径上的每一条边都是路径中的最短边。所以首先用floyd求出点到点的最短路径,然后枚举每条边判断是否是最短路径上的边,若是,则加入到新建的图中,权值为1。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<cmath>
  6 #include<algorithm>
  7 #include<queue>
  8 #define inf 0x7fffffff
  9 using namespace std;
 10 const int maxn=100+10;
 11 
 12 int n,from,to;
 13 int dist[maxn][maxn],an[maxn][maxn];
 14 int d[maxn],graph[maxn][maxn];
 15 
 16 int bfs()
 17 {
 18     memset(d,0,sizeof(d));
 19     d[from]=1;
 20     queue<int> Q;
 21     Q.push(from);
 22     while (!Q.empty())
 23     {
 24         int u=Q.front() ;Q.pop() ;
 25         for (int v=0 ;v<n ;v++)
 26         {
 27             if (!d[v] && graph[u][v]>0)
 28             {
 29                 d[v]=d[u]+1;
 30                 Q.push(v);
 31                 if (v==to) return 1;
 32             }
 33         }
 34     }
 35     return 0;
 36 }
 37 
 38 int dfs(int u,int flow)
 39 {
 40     if (u==to || flow==0) return flow;
 41     int cap=flow;
 42     for (int v=0 ;v<n ;v++)
 43     {
 44         if (d[v]==d[u]+1 && graph[u][v]>0)
 45         {
 46             int x=dfs(v,min(cap,graph[u][v]));
 47             cap -= x;
 48             graph[u][v] -= x;
 49             graph[v][u] += x;
 50             if (cap==0) return flow;
 51         }
 52     }
 53     return flow-cap;
 54 }
 55 
 56 int dinic()
 57 {
 58     int sum=0;
 59     while (bfs()) sum += dfs(from,inf);
 60     return sum;
 61 }
 62 
 63 int main()
 64 {
 65     while (scanf("%d",&n)!=EOF)
 66     {
 67         for (int i=0 ;i<n ;i++)
 68         {
 69             for (int j=0 ;j<n ;j++)
 70             {
 71                 scanf("%d",&an[i][j]);
 72                 dist[i][j]=an[i][j];
 73             }
 74             dist[i][i]=an[i][i]=0;
 75         }
 76         scanf("%d%d",&from,&to);
 77         if (from==to) {printf("inf\n");continue; }
 78         for (int k=0 ;k<n ;k++)
 79         {
 80             for (int i=0 ;i<n ;i++) if (i!=k)
 81             {
 82                 for (int j=0 ;j<n ;j++) if (j!=k && j!=i)
 83                 {
 84                     if (dist[i][k]!=-1 && dist[k][j]!=-1 &&
 85                         (dist[i][j]==-1 || dist[i][j]>dist[i][k]+dist[k][j]))
 86                             dist[i][j]=dist[i][k]+dist[k][j];
 87                 }
 88             }
 89         }
 90         //cout<<"dist[from][to]= "<<dist[from][to]<<endl;
 91         if (dist[from][to]==-1) {printf("0\n");continue; }
 92         memset(graph,0,sizeof(graph));
 93         for (int i=0 ;i<n ;i++)
 94         {
 95             for (int j=0 ;j<n ;j++)
 96             {
 97                 if (i!=j && dist[from][to]!=-1 && dist[from][i]!=-1 && dist[j][to]!=-1 && an[i][j]!=-1 &&
 98                     dist[from][to]==dist[from][i]+an[i][j]+dist[j][to])
 99                         graph[i][j]=1;
100             }
101         }
102         printf("%d\n",dinic());
103     }
104     return 0;
105 }

 

posted @ 2015-02-25 16:05  huangxf  阅读(726)  评论(0编辑  收藏  举报