标签:
8 9 1 2 2 2 3 2 2 4 1 3 5 3 4 5 4 5 8 1 1 6 2 6 7 5 7 8 1
2 6
最短路,最小割
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (100000007) #define MAXN (2000+10) #define MAXM (60000*2+10) typedef long long ll; ll mul(ll a,ll b){return (a*b)%F;} ll add(ll a,ll b){return (a+b)%F;} ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;} void upd(ll &a,ll b){a=(a%F+b%F)%F;} class Max_flow { public: int n,s,t; int q[MAXN]; int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size; void addedge(int u,int v,int w) { edge[++size]=v; weight[size]=w; next[size]=pre[u]; pre[u]=size; } void addedge2(int u,int v,int w){addedge(u,v,w);addedge(v,u,0);} bool b[MAXN]; int d[MAXN]; bool SPFA(int s,int t) { For(i,n) d[i]=INF; MEM(b) d[q[1]=s]=0; b[s]=1; int head=1,tail=1; while (head<=tail) { int now=q[head++]; Forp(now) { int &v=edge[p]; if (weight[p]&&!b[v]) { d[v]=d[now]+1; b[v]=1,q[++tail]=v; } } } return b[t]; } int iter[MAXN]; int dfs(int x,int f) { if (x==t) return f; Forpiter(x) { int v=edge[p]; if (weight[p]&&d[x]<d[v]) { int nowflow=dfs(v,min(weight[p],f)); if (nowflow) { weight[p]-=nowflow; weight[p^1]+=nowflow; return nowflow; } } } return 0; } int max_flow(int s,int t) { int flow=0; while (SPFA(s,t)) { For(i,n) iter[i]=pre[i]; int f; while (f=dfs(s,INF)) flow+=f; } return flow; } void mem(int _n,int _s,int _t) { n=_n,s=_s,t=_t; size=1; MEM(pre) } }S; class SPFA { public: void mem() { MEM(pre) MEM(edge) MEM(pre) MEM(weight) size=1; } int q[MAXN*100]; int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size; void addedge(int u,int v,int w) { edge[++size]=v; weight[size]=w; next[size]=pre[u]; pre[u]=size; } void addedge2(int u,int v,int w){addedge(u,v,w);addedge(v,u,w);} int d[MAXN]; bool b[MAXN]; int spfa(int s,int t) { MEM(b) MEMI(d) b[s]=1; d[s]=0; int head=1,tail=1;q[1]=1; while(head<=tail) { int now=q[head++]; b[now]=0; Forp(now) { int v=edge[p]; if (d[now]+weight[p]<d[v]) { d[v]=d[now]+weight[p]; if (!b[v]) { b[v]=1,q[++tail]=v; } } } } return d[t]; } }S1,S2; class link_table { public: void mem() { MEM(pre) MEM(edge) MEM(pre) MEM(weight) size=1; } int q[MAXN*100]; int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size; void addedge(int u,int v,int w) { edge[++size]=v; weight[size]=w; next[size]=pre[u]; pre[u]=size; } void addedge2(int u,int v,int w){addedge(u,v,w);addedge(v,u,w);} }St; int n,m; int a[MAXM],b[MAXM],l[MAXM]; int main() { // freopen("G.in","r",stdin); // freopen(".out","w",stdout); while(scanf("%d%d",&n,&m)==2) { For(i,m) scanf("%d%d%d",&a[i],&b[i],&l[i]); S1.mem(); For(i,m) { S1.addedge2(a[i],b[i],l[i]); } S1.spfa(1,n); S.mem(n,1,n); S2.mem(); For(i,m) { if (S1.d[a[i]]+l[i]==S1.d[b[i]]) S.addedge2(a[i],b[i],1),S2.addedge(a[i],b[i],1); else if (S1.d[a[i]]-l[i]==S1.d[b[i]]) S.addedge2(b[i],a[i],1),S2.addedge(b[i],a[i],1); } int t=S.max_flow(1,n); cout<<t<<' '; cout<<m-S2.spfa(1,n)<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 5294(Tricks Device-最短路最小割)[Template:SPFA]
标签:
原文地址:http://blog.csdn.net/nike0good/article/details/47681791