hdoj--3491--Thieves(最小割点集)



Thieves

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 1535    Accepted Submission(s): 692

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
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 1010
#define MAXM 1000000
#define INF 0x3f3f3f3f
int dis[MAXN],vis[MAXN],map[MAXN][MAXN],cur[MAXN];
int n,S,H,m,cnt,head[MAXN];
struct node
{
	int u,v,cap,flow,next;
}edge[MAXM];
void add(int a,int b,int c)
{
	node E={a,b,c,0,head[a]};
	edge[cnt]=E;
	head[a]=cnt++;
	node E1={b,a,0,0,head[b]};
	edge[cnt]=E1;
	head[b]=cnt++;
}
void getmap()
{
	cnt=0;
	memset(head,-1,sizeof(head));
	scanf("%d%d%d%d",&n,&m,&S,&H);
	for(int i=1;i<=n;i++)
	{
		int a;
		scanf("%d",&a);
		if(i==S||i==H)
		add(i,i+n,INF);
		else
		add(i,i+n,a);
	}
	for(int i=0;i<m;i++)
	{
		int a,b;
		scanf("%d%d",&a,&b);
		add(a+n,b,INF);
		add(b+n,a,INF);
	}
}
bool BFS(int s,int t)
{
	memset(dis,-1,sizeof(dis));
	memset(vis,0,sizeof(vis));
	queue<int>q;
	q.push(s);
	vis[s]=1;
	dis[s]=0;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=edge[i].next)
		{
			node E=edge[i];
			if(!vis[E.v]&&E.cap>E.flow)
			{
				dis[E.v]=dis[E.u]+1;
				vis[E.v]=1;
				if(E.v==t) return true;
				q.push(E.v);
			}
		}
	}
	return false;
}
int DFS(int x,int a,int e)
{
	if(a==0||x==e) return a;
	int flow=0,f;
	for(int &i=cur[x];i!=-1;i=edge[i].next)
	{
		node &E=edge[i];
		if(dis[x]+1==dis[E.v]&&(f=DFS(E.v,min(E.cap-E.flow,a),e))>0)
		{
			a-=f;
			flow+=f;
			edge[i].flow+=f;
			edge[i^1].flow-=f;
			if(a==0) break;
		}
	}
	return flow;
}
int MAXflow(int s,int t)
{
	int flow=0;
	while(BFS(s,t))
	{
		memcpy(cur,head,sizeof(head));
		flow+=DFS(s,INF,t);
	}
	return flow;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		getmap();
		printf("%d\n",MAXflow(S,H+n));
	}
	return 0;
}
时间: 2024-12-27 17:05:39

hdoj--3491--Thieves(最小割点集)的相关文章

hdoj 3491 Thieves 【最小割 + 拆点】

题目:hdoj 3491 Thieves 题意:给出一个无向图,然后有个出发城市s,结束城市 t ,然后每个点有流量限制,问你最少用多少的人能够使得 s 到 t 没有流量. 分析:题意是抽象出来的,但是很明显看出来是求最小割.难点有2 1:无向图,所以要建双向边 2:点有流量限制,所以要拆点,拆成两个点,然后这两点的容量为点的限制,图中点的连接设置流量为inf,保证割不掉,只能从点之间割. AC代码: #include <cstdio> #include <cstring> #in

ACM/ICPC 之 Dinic+枚举最小割点集(可做模板)(POJ1815)

最小割的好题,可用作模板. //Dinic+枚举字典序最小的最小割点集 //Time:1032Ms Memory:1492K #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> using namespace std; #define MAXN 205 #define INF 0x3f3f3f3f int N, S, T

poj--1815--Friendship(最小割点集)(枚举求最小字典序)

 Friendship Time Limit: 2000MS   Memory Limit: 20000KB   64bit IO Format: %I64d & %I64u Submit Status Description In modern society, each person has his own friends. Since all the people are very busy, they communicate with each other only by phone

【POJ3659】【USACO 2008 Jan Gold】 3.Cell Phone Network 树上最小支配集/贪心 两种做法

题意:求树上最小支配集 最小支配集:点集,即每个点可以"支配"到相邻点,求最少点数可以使所有点被支配. 图上的最小支配集是NP的,但是树上的可以DP做,是O(n)的. 暴力做就好了, f[i]表示此 点被选时的子树全支配的最小代价 g[i]表示其父亲节 点被选时的子树全支配的最小代价 h[i]表示其某子节 点被选时的子树全支配的最小代价 然后暴力转移. (v是子节点) f[x]=∑(min(f[v],min(g[v],h[v])))+1; g[x]=∑(min(f[v],h[v]));

POJ3659 Cell Phone Network(树上最小支配集:树型DP)

题目求一棵树的最小支配数. 支配集,即把图的点分成两个集合,所有非支配集内的点都和支配集内的某一点相邻. 听说即使是二分图,最小支配集的求解也是还没多项式算法的.而树上求最小支配集树型DP就OK了. 树上的每个结点作为其子树的根可以有三个状态: 不属于支配集且还没被支配 不属于支配集但被其孩子支配 属于支配集 那么就是用dp[u][1\2\3]来表示动归的状态. 123转移该怎么转移就怎么转移..最后的结果就是min(dp[root][2],dp[root][3]). 要注意的是对于有些结点前2

POJ2226 Muddy Fields(二分图最小点覆盖集)

题目给张R×C的地图,地图上*表示泥地..表示草地,问最少要几块宽1长任意木板才能盖住所有泥地,木板可以重合但不能盖住草地. 把所有行和列连续的泥地(可以放一块木板铺满的)看作点且行和列连续泥地分别作为XY部,每一块泥地看作边.这样就构造出了一个二分图. 那么,问题就是在这个二分图中就是选出最少的点覆盖所有的边,即二分图最小点覆盖集,而二分图最小点覆盖集=二分图最大匹配. 1 #include<cstdio> 2 #include<cstring> 3 #include<qu

hdu 1151 或 poj 1422 二分图 最小点覆盖集

最小点覆盖集的裸题,只要“拆点建边”然后求出最大匹配,则:最小点覆盖集的大小 = 点数 - 最大匹配 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 121; 7 const int M = 5000; 8 bool visit[N]; 9 int mark[N]; 10 int head[N]; 11 int

Jewelry Exhibition(最小点覆盖集)

Jewelry Exhibition 时间限制: 1 Sec  内存限制: 64 MB提交: 3  解决: 3[提交][状态][讨论版] 题目描述 To guard the art jewelry exhibition at night, the security agency has decided to use a new laser beam system, consisting of sender-receiver pairs. Each pair generates a strip o

数据库求闭包,求最小函数依赖集,求候选码,判断模式分解是否为无损连接,3NF,BCNF

1.说白话一点:闭包就是由一个属性直接或间接推导出的所有属性的集合. 例如:f={a->b,b->c,a->d,e->f}:由a可直接得到b和d,间接得到c,则a的闭包就是{a,b,c,d} 2. 候选码的求解理论和算法 对于给定的关系R(A1,A2,-An)和函数依赖集F,可将其属性分为4类: L类  仅出现在函数依赖左部的属性. R 类  仅出现在函数依赖右部的属性. N 类  在函数依赖左右两边均未出现的属性. LR类  在函数依赖左右两边均出现的属性. 定理:对于给定的关系

POJ 3659 Cell Phone Network(树的最小支配集)(贪心)

Cell Phone Network Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6781   Accepted: 2429 Description Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires hi