标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1285 Accepted Submission(s): 302
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #include<vector> 6 7 using namespace std; 8 9 const int MAXN=2010; 10 const int INF=0x3f3f3f3f; 11 12 struct Edge1 13 { 14 int to,cost; 15 }; 16 struct Edge2 17 { 18 int to,cap,rev; 19 }; 20 21 vector<Edge1>edge1[MAXN]; 22 vector<Edge2>edge2[MAXN]; 23 24 void addedge1(int from,int to,int cost) 25 { 26 edge1[from].push_back((Edge1){to,cost}); 27 edge1[to].push_back((Edge1){from,cost}); 28 } 29 void addedge2(int from,int to,int cap) 30 { 31 edge2[from].push_back((Edge2){to,cap,edge2[to].size()}); 32 edge2[to].push_back((Edge2){from,0,edge2[from].size()-1}); 33 } 34 35 int iter[MAXN]; 36 int level[MAXN]; 37 int in[MAXN]; //求割的时候 38 int vis[MAXN]; 39 int dis[MAXN]; 40 int cnt[MAXN]; 41 42 void init(int N) 43 { 44 for(int i=0;i<=N;i++) 45 { 46 edge1[i].clear(); 47 edge2[i].clear(); 48 } 49 } 50 51 void spfa(int N) 52 { 53 for(int i=1;i<=N;i++) 54 { 55 dis[i]=INF; 56 cnt[i]=INF; 57 } 58 memset(vis,false,sizeof(vis)); 59 60 queue<int>que; 61 while(!que.empty()) 62 que.pop(); 63 64 int str=1; 65 que.push(str); 66 vis[str]=true; 67 dis[str]=0; 68 cnt[str]=0; 69 70 while(!que.empty()) 71 { 72 int u=que.front(); 73 que.pop(); 74 vis[u]=false; //重要 75 for(int i=0;i<edge1[u].size();i++) 76 { 77 Edge1 &e=edge1[u][i]; 78 if(dis[e.to]==dis[u]+e.cost) 79 cnt[e.to]=min(cnt[e.to],cnt[u]+1); 80 if(dis[e.to]>dis[u]+e.cost) 81 { 82 dis[e.to]=dis[u]+e.cost; 83 cnt[e.to]=cnt[u]+1; 84 if(!vis[e.to]) 85 { 86 vis[e.to]=true; 87 que.push(e.to); 88 } 89 } 90 } 91 } 92 return ; 93 } 94 95 void build_group(int N) 96 { 97 for(int i=1;i<=N;i++) 98 { 99 for(int j=0;j<edge1[i].size();j++) 100 { 101 Edge1 &e=edge1[i][j]; 102 if(dis[e.to]==dis[i]+e.cost) 103 addedge2(i,e.to,1); 104 } 105 } 106 } 107 108 void bfs() 109 { 110 memset(level,-1,sizeof(level)); 111 queue<int>que2; 112 while(!que2.empty()) 113 que2.pop(); 114 int s=1; 115 que2.push(s); 116 level[1]=0; 117 while(!que2.empty()) 118 { 119 int u=que2.front(); 120 que2.pop(); 121 for(int i=0;i<edge2[u].size();i++) 122 { 123 Edge2 &e=edge2[u][i]; 124 if(e.cap>0&&level[e.to]<0) 125 { 126 level[e.to]=level[u]+1; 127 que2.push(e.to); 128 } 129 } 130 } 131 } 132 133 int dfs(int u,int T,int f) 134 { 135 if(u==T) 136 return f; 137 for(int &i=iter[u];i<edge2[u].size();i++) 138 { 139 Edge2 &e=edge2[u][i]; 140 if(e.cap>0&&level[e.to]>level[u]) 141 { 142 int d=dfs(e.to,T,min(e.cap,f)); 143 if(d>0) 144 { 145 e.cap-=d; 146 edge2[e.to][e.rev].cap+=d; 147 return d; 148 } 149 } 150 } 151 return 0; 152 } 153 154 /* 155 int solve(int N) 156 { 157 for(int i=1;i<=N;i++) 158 in[i]=false; 159 for(int i=1;i<=N;i++) 160 { 161 if(level[i]>=0) 162 in[i]=true; 163 } 164 165 int res=0; 166 167 for(int i=1;i<=N;i++) 168 { 169 if(!in[i]) 170 continue; 171 for(int j=0;j<edge2[i].size();j++) 172 { 173 Edge2 &e=edge2[i][j]; 174 if(!in[e.to]) 175 res++; 176 } 177 } 178 return res; 179 } 180 */ 181 182 int max_flow(int N) 183 { 184 int flow=0; 185 while(1) 186 { 187 bfs(); 188 if(level[N]<0) 189 break; 190 memset(iter,0,sizeof(iter)); 191 int f; 192 while(f=dfs(1,N,INF)) 193 { 194 flow+=f; 195 } 196 } 197 //return solve(N); 198 return flow; 199 } 200 201 int main() 202 { 203 int N,M; 204 while(~scanf("%d%d",&N,&M)) 205 { 206 init(N); 207 208 for(int i=1;i<=M;i++) 209 { 210 int from,to,cost; 211 scanf("%d%d%d",&from,&to,&cost); 212 addedge1(from,to,cost); 213 } 214 215 spfa(N); 216 217 build_group(N); 218 219 int cut=max_flow(N); 220 221 printf("%d %d\n",cut,M-cnt[N]); 222 } 223 return 0; 224 }
标签:
原文地址:http://www.cnblogs.com/-maybe/p/4668441.html