HDU 3987 Harry Potter and the Forbidden Forest

Description

Harry Potter notices some Death Eaters try to slip into Castle. The Death Eaters hide in the most depths of Forbidden Forest. Harry need stop them as soon as. 

The Forbidden Forest is mysterious. It consists of N nodes numbered from 0 to N-1. All of Death Eaters stay in the node numbered 0. The position of Castle is node n-1. The nodes connected by some roads. Harry need block some roads by magic and he want to minimize the cost. But it’s not enough, Harry want to know how many roads are blocked at least.

Input

Input consists of several test cases. 

The first line is number of test case. 

Each test case, the first line contains two integers n, m, which means the number of nodes and edges of the graph. Each node is numbered 0 to n-1. 

Following m lines contains information about edges. Each line has four integers u, v, c, d. The first two integers mean two endpoints of the edges. The third one is cost of block the edge. The fourth one means directed (d = 0) or undirected (d = 1). 

Technical Specification 

1. 2 <= n <= 1000 
2. 0 <= m <= 100000 
3. 0 <= u, v <= n-1 
4. 0 < c <= 1000000 
5. 0 <= d <= 1 

Output

For each test case: 
Output the case number and the answer of how many roads are blocked at least. 

Sample Input

3

4 5
0 1 3 0
0 2 1 0
1 2 1 1
1 3 1 1
2 3 3 1

6 7
0 1 1 0
0 2 1 0
0 3 1 0
1 4 1 0
2 4 1 0
3 5 1 0
4 5 2 0

3 6
0 1 1 0
0 1 2 0
1 1 1 1
1 2 1 0
1 2 1 0
2 1 1 1

Sample Output

Case 1: 3
Case 2: 2
Case 3: 2

这题是求割边最小的最小割问题,我们要了解两个性质,一个是最小割边一定满流,满流的不一定是最小割边。还有一个就是最大流等于最小割。

我们先求一遍最大流,这时满流的就可能是最小割边,我们要求最少有多少条边,就相当于求最小割就可以转换成求最大流,也就是把满流的那条边的容量设为1,其他的设为无穷,这时最大流的值也就是最小割的值就等于最小割边数(因为最小割就相当于最小割去多少容量,而1容量就代表一条边)。

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

const int maxn=1100;
const int maxm=500000+10;

const int INF=0x3f3f3f3f;

int min(int x,int y)
{
	if(x<y)
		return x;
	else
		return y;
}

struct node {  
    int u, v, cap, flow, next;  
};  
node edge[maxm];  
int dist[maxn], head[maxn], cur[maxn];  
bool vis[maxn];  
int cnt;

void init(){  
    cnt = 0;  
    memset(head, -1, sizeof(head));  
}  
  
void add(int u, int v, int w){  
	edge[cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].cap=w;
	edge[cnt].flow=0;
	edge[cnt].next=head[u];
    head[u] = cnt++;
	edge[cnt].u=v;
	edge[cnt].v=u;
	edge[cnt].cap=0;
	edge[cnt].flow=0;
	edge[cnt].next=head[v];  
    head[v] = cnt++;  
}  

bool BFS(int st, int ed){
    queue<int>q;
    memset(vis, 0 ,sizeof(vis));
    memset(dist, -1, sizeof(dist));
    q.push(st);
    dist[st] = 0;
    vis[st] = 1;
    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){  
                vis[E.v] = 1;  
                dist[E.v] = dist[u] + 1;  
                if(E.v == ed) return true;  
                q.push(E.v);  
            }  
        }  
    }  
    return false;  
}  

int DFS(int x, int ed, int a){  
    if(a == 0 || x == ed)  
        return a;  
    int flow = 0, f;  
    for(int &i = cur[x]; i != -1; i = edge[i].next){  
        node &E = edge[i];
        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
            E.flow += f;
            edge[i ^ 1].flow -= f;
            a -= f;
            flow += f;
            if(a == 0) break;
        }
    }
    return flow;  
}  
  
int maxflow(int st, int ed){  
    int flow = 0;  
    while(BFS(st, ed)){  
        memcpy(cur, head, sizeof(head));  
        flow += DFS(st, ed, INF);  
    }  
    return flow;  
}

int main()
{
	int t,icase;
	scanf("%d",&t);
	for(icase=1;icase<=t;icase++)
	{
		init();
		int n,m;
		scanf("%d%d",&n,&m);
		int i,j;
		for(i=0;i<m;i++)
		{
			int u,v,c,d;
			scanf("%d%d%d%d",&u,&v,&c,&d);
			add(u,v,c);
			if(d==1)
				add(v,u,c);
		}
		maxflow(0,n-1);
		for(i=0;i<cnt;i+=2)
		{
			if(edge[i].cap==edge[i].flow)
			{
				edge[i].cap=1;
				edge[i].flow=0;
				edge[i^1].cap=0;
				edge[i^1].flow=0;
			}
			else
			{
				edge[i].cap=INF;
				edge[i].flow=0;
				edge[i^1].cap=0;
				edge[i^1].flow=0;
			}
		}
		int ans=maxflow(0,n-1);
		printf("Case %d: %d\n",icase,ans);
	}
	return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值