http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11545
只是要求不经过相同的边,那么每次找出一条增广路T--,判断T<=0即可.
在加边的时候注意要c<=C的时候才需要加边.
邻接表:
1 # include <cstdio> 2 # include <cstring> 3 # include <algorithm> 4 # include <iostream> 5 using namespace std; 6 7 int list[1020],dis[1020],gap[1020],node; 8 int source,sink,Vs,inf = 1 << 30; 9 int N,M,T,C; 10 struct E 11 { 12 int to,c,next; 13 }edg[100002]; 14 15 void addedg(int from,int to,int value) 16 { 17 edg[node].to = to,edg[node].c = value,edg[node].next = list[from],list[from] = node++; 18 edg[node].to = from,edg[node].c = 0,edg[node].next = list[to],list[to] = node ++; 19 } 20 21 int dfs(int src,int aug) 22 { 23 if(src == sink) return aug; 24 25 int flow = 0,mid_d = Vs-1; 26 for(int j = list[src];j != -1; j = edg[j].next) 27 if(edg[j].c) 28 { 29 if(dis[src] == dis[edg[j].to]+1) 30 { 31 int t = dfs(edg[j].to,min(aug-flow,edg[j].c)); 32 33 edg[j].c -= t; 34 edg[j^1].c += t; 35 flow += t; 36 if(dis[source] >= Vs) return flow; 37 if(aug == flow) break; 38 } 39 mid_d = min(mid_d,dis[edg[j].to]); 40 } 41 if(!flow) 42 { 43 if(!(--gap[dis[src]])) dis[source] = Vs; 44 dis[src] = mid_d+1; 45 ++gap[dis[src]]; 46 } 47 return flow; 48 } 49 50 bool maxflow_sap(int src,int ed) 51 { 52 int ans = 0; 53 memset(gap,0,sizeof(gap)); 54 memset(dis,0,sizeof(dis)); 55 gap[0] = Vs = ed; 56 source = src, sink = ed; 57 58 while(dis[source] < Vs) 59 { 60 //printf("%d %d ",T,ans); 61 if(dfs(source,inf)) 62 T--; 63 if(T<=0) break; 64 } 65 if(T<=0) return true; 66 return false; 67 68 } 69 70 int main() 71 { 72 //freopen("a.txt","r",stdin); 73 while(~scanf("%d%d%d%d",&N,&M,&T,&C)) 74 { 75 int i,j; 76 node = 0; 77 memset(list,-1,sizeof(list)); 78 for(i = 0;i < M;i++) 79 { 80 int x,y,c; 81 scanf("%d%d%d",&x,&y,&c); 82 // printf("%d%d%d ",x,y,c); 83 if(c<=C) addedg(x,y,c); 84 } 85 if(maxflow_sap(1,N)) printf("YES "); 86 else printf("NO "); 87 } 88 return 0; 89 }