标签:har include sam from zoj 开始 getc class gis
约定:
2 <= N <= 50
1 <= M <= 2450
1 <= T <= 50
1 <= X,Y <= N
X != Y
1 <= Z <= 50
正解:最大流。
最大流+按时间拆点的裸套路。
1 #include <bits/stdc++.h> 2 #define il inline 3 #define RG register 4 #define ll long long 5 #define N (2000010) 6 #define inf (1<<30) 7 8 using namespace std; 9 10 struct edge{ int nt,to,flow,cap; }g[N]; 11 struct e{ int u,v,w; }e[2500]; 12 13 int id[52][310],head[N],q[N],d[N],S,T,n,m,t,num,cnt,ans,flow; 14 15 il int gi(){ 16 RG int x=0,q=1; RG char ch=getchar(); 17 while ((ch<‘0‘ || ch>‘9‘) && ch!=‘-‘) ch=getchar(); 18 if (ch==‘-‘) q=-1,ch=getchar(); 19 while (ch>=‘0‘ && ch<=‘9‘) x=x*10+ch-48,ch=getchar(); 20 return q*x; 21 } 22 23 il void insert(RG int from,RG int to,RG int cap){ 24 g[++num]=(edge){head[from],to,0,cap},head[from]=num; 25 g[++num]=(edge){head[to],from,0,0},head[to]=num; return; 26 } 27 28 il int bfs(RG int S,RG int T){ 29 for (RG int i=1;i<=cnt;++i) d[i]=0; 30 RG int h=0,t=1; q[t]=S,d[S]=1; 31 while (h<t){ 32 RG int x=q[++h],v; 33 for (RG int i=head[x];i;i=g[i].nt){ 34 v=g[i].to; 35 if (!d[v] && g[i].cap>g[i].flow){ 36 d[v]=d[x]+1,q[++t]=v; 37 if (v==T) return 1; 38 } 39 } 40 } 41 return d[T]; 42 } 43 44 il int dfs(RG int x,RG int T,RG int a){ 45 if (!a || x==T) return a; RG int v,f,flow=0; 46 for (RG int i=head[x];i;i=g[i].nt){ 47 v=g[i].to; 48 if (d[v]==d[x]+1 && g[i].cap>g[i].flow){ 49 f=dfs(v,T,min(a,g[i].cap-g[i].flow)); 50 if (!f){ d[v]=-1; continue; } 51 g[i].flow+=f,g[i^1].flow-=f; 52 flow+=f,a-=f; if (!a) return flow; 53 } 54 } 55 return flow; 56 } 57 58 il int maxflow(RG int S,RG int T){ 59 RG int flow=0; 60 while (bfs(S,T)) flow+=dfs(S,T,inf); 61 return flow; 62 } 63 64 int main(){ 65 #ifndef ONLINE_JUDGE 66 freopen("bluemary.in","r",stdin); 67 freopen("bluemary.out","w",stdout); 68 #endif 69 n=gi(),m=gi(),t=gi(),num=1,S=++cnt,T=++cnt; 70 for (RG int i=1;i<=m;++i) e[i].u=gi(),e[i].v=gi(),e[i].w=gi(); 71 for (RG int i=1;i<=n;++i) id[i][0]=++cnt; 72 insert(S,id[1][0],t),insert(id[n][0],T,inf); 73 while (++ans){ 74 for (RG int i=1;i<=n;++i) id[i][ans]=++cnt,insert(id[i][ans-1],id[i][ans],inf); 75 for (RG int i=1;i<=m;++i) insert(id[e[i].u][ans-1],id[e[i].v][ans],e[i].w); 76 insert(id[n][ans],T,inf),flow+=maxflow(S,T); if (flow==t) break; 77 } 78 cout<<ans; return 0; 79 }
bzoj1570 [JSOI2008]Blue Mary的旅行
标签:har include sam from zoj 开始 getc class gis
原文地址:http://www.cnblogs.com/wfj2048/p/7655402.html