题目大意:
明显的网络流问题,直接到魔板代码就可以ac,具体内容就不重复了,其实我看完样例数据根本就没读题。
代码:
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#define maxn 2210
#define inf 0x3f3f3f3f
using namespace std;
struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
int n;int m;
vector<Edge>edges;
vector<int>G[maxn];
int a[maxn];
int p[maxn];
void init(int n)
{
for(int i=0;i<n;i++)G[i].clear();
edges.clear();
}
void add_edge(int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,0));
edges.push_back(Edge(to,from,0,0));
int lm=edges.size();
G[from].push_back(lm-2);
G[to].push_back(lm-1);
}
int maxflow(int s,int t)
{
int flow=0;
while(1)
{
memset(a,0,sizeof(a));
queue<int>Q;
Q.push(s);
a[s]=inf;
while(!Q.empty())
{
int x=Q.front();Q.pop();
for(int i=0;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(!a[e.to]&&e.cap>e.flow)
{
p[e.to]=G[x][i];
a[e.to]=min(a[x],e.cap-e.flow);
Q.push(e.to);
}
}
if(a[t])break;
}
if(!a[t])break;
for(int u=t;u!=s;u=edges[p[u]].from)
{
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
}
flow+=a[t];
}
return flow;
}
int main()
{
int test=0;
cin>>test;
for(int xcx=1;xcx<=test;xcx++)
{
scanf("%d%d",&n,&m);
init(n);
memset(p,0,sizeof(p));
for(int i=1;i<=m;i++)
{
int la,lb,lc;
scanf("%d%d%d",&la,&lb,&lc);
add_edge(la,lb,lc);
}
printf("Case %d: %d\n",xcx,maxflow(1,n));
}
}