题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4289
Problem Description You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from one city (the source) to another one (the destination). You know their date, source and destination, and they are using the highway network.
Input There are several test cases.
Output For each test case you should output exactly one line, containing one integer, the sum of cost of your selected set.
Sample Input Sample Output |
题目大意:类似于:HDU-4292,多组输入。
开局n个城市,m条路;然后是每组的 起点s市 和 终点 t市
接下来是路过每个城市的花费 cost[i] 。然后是m条路(双向道路),我们要找到s到m的最大流
很明显的拆点,道路之间的流量限制为INF,城市的流量限制为cost[i];向前星就很容易建图,水题,套板子直接就过了,TLE数组加个0(反正是这样过的 23333....)
ac:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
//#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<fstream>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define mod 998244353
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b)
#define clean(a,b) memset(a,b,sizeof(a))// 水印
//std::ios::sync_with_stdio(false);
struct node{
int v,w,nxt;
node(int _v=0,int _w=0,int _nxt=0):
v(_v),w(_w),nxt(_nxt){}
}edge[100005<<1];
int head[100005],e;
int dis[100005];
int n,m;
int s,t;
void intt()
{
clean(head,-1);
clean(dis,-1);
e=0;
}
void add(int u,int v,int w)
{
edge[e]=node(v,w,head[u]);
head[u]=e++;
edge[e]=node(u,0,head[v]);
head[v]=e++;
}
bool bfs()
{
clean(dis,-1);
dis[s]=0;
queue<int> que;
que.push(s);
while(que.size())
{
int u=que.front();
que.pop();
if(u==t)
return 1;
for(int i=head[u];i+1;i=edge[i].nxt)
{
int temp=edge[i].v;
if(dis[temp]<0&&edge[i].w>0)
{
dis[temp]=dis[u]+1;
que.push(temp);
}
}
}
return 0;
}
int dfs(int u,int low)
{
if(u==t)
return low;
int res=0;
for(int i=head[u];i+1;i=edge[i].nxt)
{
int temp=edge[i].v;
if(dis[temp]==dis[u]+1&&edge[i].w>0)
{
int d=dfs(temp,min(low-res,edge[i].w));
edge[i].w-=d;
edge[i^1].w+=d;
res=res+d;
if(res==low)
break;
}
}
return res;
}
void Dinic()
{
int ans=0,res;
while(bfs())
{
while(res=dfs(s,INF))
ans=ans+res;
}
cout<<ans<<endl;
}
int main()
{
while(cin>>n>>m)
{
intt();
cin>>s>>t;
int cost;
for(int i=1;i<=n;++i)
{
cin>>cost;
add(i,i+200,cost);
add(i+200,i,cost);
}
int a,b;
for(int i=1;i<=m;++i)
{
cin>>a>>b;
add(a+200,b,INF);
add(b+200,a,INF);
}
add(0,s,INF);
add(t+200,401,INF);
s=0,t=401;
Dinic();
}
}