对于30%的数据,N ≤ 20,M ≤ 120。
对于100%的数据,N ≤ 200,M ≤ 20000。
Day1
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define N 20003 using namespace std; int n,m,point[N*2],next[N*4],v[N*4],remain[N*4],c[N*4],tot=-1; int minn,flow,dis[N],can[N],laste[N]; const int inf=1e9; void add(int x,int y,int z,int k) { tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z; c[tot]=k; tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0; c[tot]=-k; } int addflow(int s,int t) { int now=t; int ans=inf; while(now!=s) { ans=min(ans,remain[laste[now]]); now=v[laste[now]^1]; } now=t; while (now!=s) { remain[laste[now]]-=ans; remain[laste[now]^1]+=ans; now=v[laste[now]^1]; } return ans; } bool spfa(int s,int t) { memset(dis,0x7f,sizeof(dis)); dis[s]=0; can[s]=1; queue<int> p; p.push(s); while (!p.empty()) { int now=p.front(); p.pop(); for (int i=point[now];i!=-1;i=next[i]) if (dis[v[i]]>dis[now]+c[i]&&remain[i]) { dis[v[i]]=dis[now]+c[i]; laste[v[i]]=i; if (!can[v[i]]) { can[v[i]]=1; p.push(v[i]); } } can[now]=0; } if (dis[t]>inf) return false; int tl=addflow(s,t); flow+=tl; minn+=tl*dis[t]; return true; } void maxflow(int s,int t) { flow=0; minn=0; while (spfa(s,t)); } int main() { memset(next,-1,sizeof(next)); memset(point,-1,sizeof(point)); scanf("%d%d",&n,&m); for (int i=2;i<=n-1;i++) add(i,i+n,1,0); for (int i=1;i<=m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); add(x+n,y,1,z); } add(1,n+1,inf,0); add(n,n+n,inf,0); maxflow(1,n+n); printf("%d %d\n",flow,minn); }