HDU - 3491 Thieves

Thieves

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1758 Accepted Submission(s): 821

Problem Description
In the kingdom of Henryy, there are N (2 <= N <= 100) cities, with M (M <= 10000) two-direct ways connecting them.
A group of thieves from abroad plan to steal the metropolitan museum in city H (It has not been demolished). However, the brave, brilliant, bright police in the kingdom have known this plan long before, and they also plan to catch the thieves. The thieves are in the city S at present. The police try to catch them on their way from S to H. Although the thieves might travel this way by more than one group, our excellent police has already gather the statistics that the number of the people needed in city I (1<=I<=N) to arrest the thieves.
The police do not want to encounter the thieves in either city S or city H.
The police finish the task with the minimum number of people. Do you know the exact number?

Input
The first line contains an integer T (T <= 10), indicating the number of the test cases.
The first line of each test case has four integers: N, the number of the cities; M, the number of the roads; S (1<=S<=N), the label of city S; H (1<=T<=N, S≠H), the label of city H.
The second line contains N integers, indicating the number of people needed in each city. The sum of these N integers is less than 10000.
Then M lines followed, each containing two integers x and y, indicating that there is a two-direct roads between city x and y. Notices that no road between city S and H.
A blank line is followed after each test case.

Output
For each test case, output a single integer in a separate line, indicating the minimum number of people the police used.

Sample Input
1
5 5 1 5
1 6 6 11 1
1 2
1 3
2 4
3 4
4 5

Sample Output
11

Source
2010 ACM-ICPC Multi-University Training Contest(6)——Host by BIT


很简单的一道最小割,因为我们在城市放人,直接把城市拆点即可,然后城市到城市之间流量为INF,跑最大流即可。

注意初末两个城市之间不能割。


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=2e3+10,M=1e5+10;
const int inf=0x3f3f3f3f;
int T,n,m,s,t,h[N],val[N];
int head[N],nex[M],to[M],w[M],tot;
inline void ade(int a,int b,int c){
	to[++tot]=b; w[tot]=c; nex[tot]=head[a]; head[a]=tot;
}
inline void add(int a,int b,int c){
	ade(a,b,c);	ade(b,a,0);
}
inline int bfs(){
	memset(h,0,sizeof h); queue<int> q;	q.push(s);	h[s]=1;
	while(q.size()){
		int x=q.front();	q.pop();
		for(int i=head[x];i;i=nex[i]){
			if(w[i]&&!h[to[i]]){
				h[to[i]]=h[x]+1;	q.push(to[i]);
			}
		}
	}
	return h[t];
}
int dfs(int x,int f){
	if(t==x)	return f;	int fl=0;
	for(int i=head[x];i&&f;i=nex[i]){
		if(w[i]&&h[to[i]]==h[x]+1){
			int mi=dfs(to[i],min(w[i],f));
			w[i]-=mi; w[i^1]+=mi; fl+=mi; f-=mi;
		}
	}
	if(!fl)	h[x]=-1;
	return fl;
}
inline int dinic(){
	int res=0;
	while(bfs())	res+=dfs(s,inf);
	return res;
}
signed main(){
	scanf("%d",&T);
	while(T--){
		tot=1;	memset(head,0,sizeof head);
		scanf("%d %d %d %d",&n,&m,&s,&t); s+=n;
		for(int i=1;i<=n;i++)	scanf("%d",&val[i]),add(i,i+n,val[i]);
		while(m--){
			int a,b;	scanf("%d %d",&a,&b);	add(a+n,b,inf);	add(b+n,a,inf);
		}
		printf("%d\n",dinic());
	}
	return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
4887次浏览 47人参与
# 你的实习产出是真实的还是包装的? #
1093次浏览 27人参与
# MiniMax求职进展汇总 #
22870次浏览 293人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
6898次浏览 36人参与
# 简历第一个项目做什么 #
31245次浏览 312人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186343次浏览 1114人参与
# 米连集团26产品管培生项目 #
4096次浏览 198人参与
# 面试紧张时你会有什么表现? #
30328次浏览 188人参与
# 简历中的项目经历要怎么写? #
309361次浏览 4150人参与
# 网易游戏笔试 #
6304次浏览 83人参与
# 职能管理面试记录 #
10682次浏览 59人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6850次浏览 154人参与
# 从哪些方向判断这个offer值不值得去? #
56695次浏览 357人参与
# 腾讯音乐求职进展汇总 #
160391次浏览 1105人参与
# 小红书求职进展汇总 #
226845次浏览 1356人参与
# AI时代,哪些岗位最容易被淘汰 #
62375次浏览 727人参与
# 你怎么看待AI面试 #
179254次浏览 1163人参与
# 正在春招的你,也参与了去年秋招吗? #
362517次浏览 2631人参与
# 你的房租占工资的比例是多少? #
92123次浏览 896人参与
# 机械求职避坑tips #
94396次浏览 567人参与
# 校招笔试 #
466168次浏览 2950人参与
# 面试官最爱问的 AI 问题是...... #
27111次浏览 834人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务