转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
War
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1534 Accepted Submission(s): 334
Problem Description
In the war between Anihc and Bandzi, Anihc has won Bangzi. At that time, Lvbin, the king of Anihc, want to start from Bangzi to beat Nebir across the channel between them. He let his army get to there as soon as possible, and there located many islands which can be used to have a break. In order to save time, the king forbid the army getting through the same pass for more than one time, but they can reach the same island for as many as times they want.
Yunfeng, the General of the army, must tell how many optimal ship routes are there to the king as soon as possible, or he will be killed. Now he asks for your help. You must help Yunfeng to save his life.
He tells you that there are N island. The islands are numbered from 1 to N(1 is Bangzi and N is Nebir, others are many islands). And there are many ways, each way contain the islands number U and V and the length W. Please output your answer.
Yunfeng, the General of the army, must tell how many optimal ship routes are there to the king as soon as possible, or he will be killed. Now he asks for your help. You must help Yunfeng to save his life.
He tells you that there are N island. The islands are numbered from 1 to N(1 is Bangzi and N is Nebir, others are many islands). And there are many ways, each way contain the islands number U and V and the length W. Please output your answer.
Input
The first line in the input file contains a single integer number T means the case number.
Each case contains a number N (N<=1500) means the number of the islands.
And then many lines follow. Each line contains three numbers: U V W (W<10000), means that the distance between island U and V is W. The input of ways are terminated by “ 0 0 0 ”.
Each case contains a number N (N<=1500) means the number of the islands.
And then many lines follow. Each line contains three numbers: U V W (W<10000), means that the distance between island U and V is W. The input of ways are terminated by “ 0 0 0 ”.
Output
Print the number of the optimal ship routes are there after each case in a line.
Sample Input
1
6
1 2 1
3 2 1
3 4 1
1 3 2
4 2 2
4 5 1
5 6 1
4 6 2
0 0 0
Sample Output
2
Author
alpc92
Source
题意:
问有几种最短路的方案。每条边只能经过一次。
分析:
跑一遍最短路,若某条边是最短路的中的边,则连一条对应的容量为1的边,然后dinic搞一下就行
1 //##################### 2 //Author:fraud 3 //Blog: http://www.cnblogs.com/fraud/ 4 //##################### 5 #include <iostream> 6 #include <sstream> 7 #include <ios> 8 #include <iomanip> 9 #include <functional> 10 #include <algorithm> 11 #include <vector> 12 #include <string> 13 #include <list> 14 #include <queue> 15 #include <deque> 16 #include <stack> 17 #include <set> 18 #include <map> 19 #include <cstdio> 20 #include <cstdlib> 21 #include <cmath> 22 #include <cstring> 23 #include <climits> 24 #include <cctype> 25 using namespace std; 26 #define XINF INT_MAX 27 #define INF 0x3FFFFFFF 28 #define MP(X,Y) make_pair(X,Y) 29 #define PB(X) push_back(X) 30 #define REP(X,N) for(int X=0;X<N;X++) 31 #define REP2(X,L,R) for(int X=L;X<=R;X++) 32 #define DEP(X,R,L) for(int X=R;X>=L;X--) 33 #define CLR(A,X) memset(A,X,sizeof(A)) 34 #define IT iterator 35 typedef long long ll; 36 typedef pair<int,int> PII; 37 typedef vector<PII> VII; 38 typedef vector<int> VI; 39 #define MAXN 1600 40 vector<PII> Map[MAXN]; 41 42 43 void init() { REP(i,MAXN) Map[i].clear(); } 44 45 int dis[MAXN]; 46 void dijkstra(int s) 47 { 48 REP(i,MAXN){dis[i]=i==s?0:INF;} 49 int vis[MAXN] = {0}; 50 priority_queue<PII, vector<PII>, greater<PII> > q; 51 q.push(MP(0,s)); 52 while(!q.empty()) 53 { 54 PII p = q.top(); q.pop(); 55 int x = p.second; 56 if(vis[x])continue; 57 vis[x] = 1; 58 for(vector<PII>::iterator it = Map[x].begin(); it != Map[x].end(); it++) 59 { 60 int y = it->first; 61 int d = it->second; 62 if(!vis[y] && dis[y] > dis[x] + d) 63 { 64 dis[y] = dis[x] + d; 65 q.push(MP(dis[y],y)); 66 } 67 } 68 } 69 } 70 struct edge{ 71 int to,cap,rev; 72 edge(int _to,int _cap,int _rev) 73 { 74 to=_to; 75 cap=_cap; 76 rev=_rev; 77 } 78 }; 79 const int MAX_V=2020; 80 vector<edge>G[MAX_V]; 81 int iter[MAX_V]; 82 int level[MAX_V]; 83 int tot=0; 84 void add_edge(int from,int to,int cap) 85 { 86 G[from].PB(edge(to,cap,G[to].size())); 87 G[to].PB(edge(from,0,G[from].size()-1)); 88 } 89 void bfs(int s,int t) 90 { 91 CLR(level,-1); 92 queue<int>q; 93 level[s]=0; 94 q.push(s); 95 while(!q.empty()) 96 { 97 int u=q.front(); 98 q.pop(); 99 for(int i=0;i<G[u].size();i++) 100 { 101 edge &e=G[u][i]; 102 if(e.cap>0&&level[e.to]<0) 103 { 104 level[e.to]=level[u]+1; 105 q.push(e.to); 106 } 107 } 108 } 109 } 110 int dfs(int v,int t,int f) 111 { 112 if(v==t)return f; 113 for(int &i=iter[v];i<G[v].size();i++) 114 { 115 edge &e=G[v][i]; 116 if(e.cap>0&&level[v]<level[e.to]) 117 { 118 int d=dfs(e.to,t,min(f,e.cap)); 119 if(d>0) 120 { 121 e.cap-=d;; 122 G[e.to][e.rev].cap+=d; 123 return d; 124 } 125 } 126 } 127 return 0; 128 } 129 int Dinic(int s,int t) 130 { 131 int flow=0; 132 for(;;) 133 { 134 bfs(s,t); 135 if(level[t]<0)return flow; 136 memset(iter,0,sizeof(iter)); 137 int f; 138 while((f=dfs(s,t,INF))>0) 139 { 140 flow+=f; 141 } 142 } 143 } 144 145 int main() 146 { 147 ios::sync_with_stdio(false); 148 int t; 149 scanf("%d",&t); 150 while(t--){ 151 int n; 152 init(); 153 scanf("%d",&n); 154 int u,v,d; 155 while(scanf("%d%d%d",&u,&v,&d)&&(u||v||d)){ 156 u--; 157 v--; 158 Map[u].PB(MP(v,d)); 159 Map[v].PB(MP(u,d)); 160 } 161 if(n==1){ 162 printf("0 "); 163 continue; 164 } 165 for(int i=0;i<n;i++)G[i].clear(); 166 dijkstra(0); 167 for(int i=0;i<n;i++){ 168 for(vector<PII>::iterator it = Map[i].begin(); it != Map[i].end(); it++) 169 { 170 int y = it->first; 171 int d = it->second; 172 if(dis[i]+d==dis[y]){ 173 add_edge(i,y,1); 174 } 175 } 176 } 177 printf("%d ",Dinic(0,n-1)); 178 } 179 180 181 return 0; 182 }