1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| #include<bits/stdc++.h>
using namespace std;
inline int Get_Int() { int num=0,bj=1; char x=getchar(); while(!isdigit(x)) {if(x=='-')bj=-1;x=getchar();} while(isdigit(x)) {num=num*10+x-'0';x=getchar();} return num*bj; }
const int maxn=20005;
struct Dinic { struct Edge { int from,to,cap,flow; Edge(int x=0,int y=0,int c=0,int f=0):from(x),to(y),cap(c),flow(f) {} }; int n,m,s,t; vector<Edge>edges; vector<int>G[maxn]; bool vst[maxn]; int dist[maxn],cur[maxn]; void init(int n) { this->n=n; edges.clear(); for(int i=1; i<=n; i++)G[i].clear(); } void AddEdge(int x,int y,int v) { edges.push_back(Edge(x,y,v,0)); edges.push_back(Edge(y,x,0,0)); m=edges.size(); G[x].push_back(m-2); G[y].push_back(m-1); } bool bfs() { fill(vst+1,vst+n+1,0); queue<int>Q; Q.push(t); vst[t]=1; while(!Q.empty()) { int Now=Q.front(); Q.pop(); for(int id:G[Now]) { Edge& e=edges[id^1]; int Next=e.from; if(!vst[Next]&&e.cap>e.flow) { vst[Next]=1; dist[Next]=dist[Now]+1; if(Next==s)return 1; Q.push(Next); } } } return vst[s]; } int dfs(int Now,int a) { if(Now==t||a==0)return a; int flow=0; for(int& i=cur[Now]; i<G[Now].size(); i++) { Edge& e=edges[G[Now][i]]; int Next=e.to; if(dist[Now]-1!=dist[Next])continue; int nextflow=dfs(Next,min(a,e.cap-e.flow)); if(nextflow>0) { e.flow+=nextflow; edges[G[Now][i]^1].flow-=nextflow; flow+=nextflow; a-=nextflow; if(a==0)break; } } return flow; } int maxflow(int s,int t) { this->s=s; this->t=t; int flow=0; while(bfs()) { memset(cur,0,sizeof(cur)); flow+=dfs(s,INT_MAX); } return flow; } } dinic;
struct Edge {int from,to,dist;} edges[400005];
int n,m,s,t,l,cnt=0;
int main() { n=Get_Int(); m=Get_Int(); for(int i=1; i<=m; i++) { int x=Get_Int(),y=Get_Int(),v=Get_Int(); edges[++cnt]=(Edge) {x,y,v}; edges[++cnt]=(Edge) {y,x,v}; } s=Get_Int(); t=Get_Int(); l=Get_Int(); dinic.init(n); for(int i=1; i<=2*m; i++)if(edges[i].dist<l)dinic.AddEdge(edges[i].from,edges[i].to,1); int ans=dinic.maxflow(s,t); dinic.init(n); for(int i=1; i<=2*m; i++)if(edges[i].dist>l)dinic.AddEdge(edges[i].from,edges[i].to,1); printf("%d\n",ans+dinic.maxflow(s,t)); return 0; }
|