【HDOJ6582】Path(最短路图,最小割)
题意:
n,m<=1e4,c<=1e9
思路:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 typedef unsigned int uint; 5 typedef unsigned long long ull; 6 typedef pair<int,int> PII; 7 typedef pair<ll,ll> Pll; 8 typedef vector<int> VI; 9 #define N 110000 10 #define M 1100000 11 #define fi first 12 #define se second 13 #define MP make_pair 14 #define pi acos(-1) 15 #define mem(a,b) memset(a,b,sizeof(a)) 16 #define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++) 17 #define per(i,a,b) for(int i=(int)a;i>=(int)b;i--) 18 #define lowbit(x) x&(-x) 19 #define Rand (rand()*(1<<16)+rand()) 20 #define id(x) ((x)<=B?(x):m-n/(x)+1) 21 #define ls p<<1 22 #define rs p<<1|1 23 24 const int MOD=1e9+7; 25 double eps=1e-6; 26 ll INF=1e14; 27 28 priority_queue<pair<ll,int> > q; 29 30 ll dis[N],dis1[N],dis2[N]; 31 int head[N],vet[N],nxt[N],len[N],vis[N],fan[N],gap[N],n,m,tot,S,T,s; 32 33 struct arr 34 { 35 int x,y,z; 36 }a[N]; 37 38 int read() 39 { 40 int v=0,f=1; 41 char c=getchar(); 42 while(c<48||57<c) {if(c=='-') f=-1; c=getchar();} 43 while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar(); 44 return v*f; 45 } 46 47 void add(int a,int b,int c) 48 { 49 nxt[++tot]=head[a]; 50 vet[tot]=b; 51 len[tot]=c; 52 head[a]=tot; 53 } 54 55 void Add(int a,int b,int c) 56 { 57 nxt[++tot]=head[a]; 58 vet[tot]=b; 59 len[tot]=c; 60 head[a]=tot; 61 62 nxt[++tot]=head[b]; 63 vet[tot]=a; 64 len[tot]=0; 65 head[b]=tot; 66 } 67 68 void dijk(int st,int op) 69 { 70 mem(vis,0); 71 rep(i,1,n) dis[i]=1e13; 72 while(!q.empty()) q.pop(); 73 q.push(MP(0,st)); dis[st]=0; 74 while(!q.empty()) 75 { 76 int u=q.top().se; 77 q.pop(); 78 if(vis[u]) continue; 79 vis[u]=1; 80 int e=head[u]; 81 while(e) 82 { 83 int v=vet[e]; 84 if(dis[u]+len[e]<dis[v]) 85 { 86 dis[v]=dis[u]+len[e]; 87 q.push(MP(-dis[v],v)); 88 } 89 e=nxt[e]; 90 } 91 } 92 if(op==1) 93 { 94 rep(i,1,n) dis1[i]=dis[i]; 95 } 96 else 97 { 98 rep(i,1,n) dis2[i]=dis[i]; 99 } 100 } 101 102 ll dfs(int u,int aug) 103 { 104 if(u==T) return aug; 105 int e=head[u],flow=0; 106 ll val=s-1; 107 while(e) 108 { 109 int v=vet[e]; 110 if(len[e]) 111 { 112 if(dis[u]==dis[v]+1) 113 { 114 int t=dfs(v,min(len[e],aug-flow)); 115 len[e]-=t; 116 len[fan[e]]+=t; 117 flow+=t; 118 if(dis[S]>=s) return flow; 119 if(aug==flow) break; 120 } 121 val=min(val,dis[v]); 122 } 123 e=nxt[e]; 124 } 125 if(!flow) 126 { 127 gap[dis[u]]--; 128 if(!gap[dis[u]]) dis[S]=s; 129 dis[u]=val+1; 130 gap[dis[u]]++; 131 } 132 return flow; 133 } 134 135 136 ll maxflow() 137 { 138 mem(gap,0); 139 mem(dis,0); 140 gap[0]=s; 141 ll ans=0; 142 while(dis[S]<s) ans+=dfs(S,INF); 143 return ans; 144 } 145 146 int main() 147 { 148 //freopen("1.in","r",stdin); 149 //freopen("1.out","w",stdout); 150 rep(i,1,20000) 151 if(i&1) fan[i]=i+1; 152 else fan[i]=i-1; 153 int cas=read(); 154 while(cas--) 155 { 156 n=read(),m=read(); 157 rep(i,1,m) 158 { 159 a[i].x=read(); 160 a[i].y=read(); 161 a[i].z=read(); 162 } 163 tot=0; 164 rep(i,1,n) head[i]=0; 165 rep(i,1,m) add(a[i].x,a[i].y,a[i].z); 166 dijk(1,1); 167 tot=0; 168 rep(i,1,n) head[i]=0; 169 rep(i,1,m) add(a[i].y,a[i].x,a[i].z); 170 dijk(n,2); 171 tot=0; 172 rep(i,1,n) head[i]=0; 173 rep(i,1,m) 174 if(dis1[a[i].x]+a[i].z+dis2[a[i].y]==dis1[n]) 175 Add(a[i].x,a[i].y,a[i].z); 176 177 178 S=1; T=n; s=n; 179 ll ans=maxflow(); 180 printf("%I64d\n",ans); 181 } 182 return 0; 183 }
null