hdu 3987 最小割

Harry Potter and the Forbidden Forest

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1155    Accepted Submission(s): 404


Problem 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,求出最大流取模,顺便就可以得到边数。 代码:
/* ***********************************************
Author :xianxingwuguan
Created Time :2014-1-25 12:19:41
File Name :3.cpp
************************************************ */

#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const __int64 inf=10000000000000LL;
const int maxn=100010;
int head[maxn],tol,dep[maxn],n;
struct node 
{
      int from,to,next;
      __int64 cap;
}edge[1000000];
void add(int u,int v,__int64 cap)
{
      edge[tol]=(node){
	     u,v,head[u],cap
      };
      head[u]=tol++;
      edge[tol]=(node){
	     v,u,head[v],0
      };
      head[v]=tol++;
}
int bfs(int s,int t)
{
       int que[maxn],front=0,rear=0;
       memset(dep,-1,sizeof(dep));
       dep[s]=0;que[rear++]=s;
       while(front!=rear)
       {
              int u=que[front++];front%=maxn;
              for(int i=head[u];i!=-1;i=edge[i].next)
              {
                     int v=edge[i].to;
                     if(edge[i].cap>0&&dep[v]==-1)
                     {
                            dep[v]=dep[u]+1;
                            que[rear++]=v;
                            rear%=maxn;
                            if(v==t)return 1;
                     }
              }
       }
       return 0;
}

__int64 dinic(int s,int t)
{
      __int64 res=0;
      while(bfs(s,t))
      {
	     int Stack[maxn],top,cur[maxn];
	     memcpy(cur,head,sizeof(head));
	     top=0;
	     int u=s;
	     while(1)
	     {
		    if(t==u)
		    {
			   __int64 min=inf;
			   int loc;
			   for(int i=0;i<top;i++)
		          if(min>edge[Stack[i]].cap)
			   {
				  min=edge[Stack[i]].cap;
				  loc=i;
			   }
			   for(int i=0;i<top;i++)
			   {
				  edge[Stack[i]].cap-=min;
				  edge[Stack[i]^1].cap+=min;
			   }
			   res+=min;
			   top=loc;
                        u=edge[Stack[top]].from;
		    }
		    for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)
			   if(dep[edge[i].to]==dep[u]+1&&edge[i].cap>0)break;
		    if(cur[u]!=-1)
		    {
			   Stack[top++]=cur[u];
			   u=edge[cur[u]].to;
		    }
		    else 
		    {
			   if(top==0)break;
			   dep[u]=-1;
			   u=edge[Stack[--top]].from;
		    }
	     }
      }
      return res;
}
int main()
{
      //freopen("data.in","r",stdin);
      //freopen("data.out","w",stdout);
      int i,j,m,T,t,p;
      __int64 k;
      scanf("%d",&T);
      for(t=1;t<=T;t++)
      {
	     scanf("%d%d",&n,&m);
	     memset(head,-1,sizeof(head));tol=0;
	     while(m--)
	     {
		    scanf("%d%d%I64d%d",&i,&j,&k,&p);
		    add(i,j,k*200001+1);
		    if(p)add(j,i,k*200001+1);
	     }
	    // cout<<tol<<endl;
	    printf("Case %d: ",t);
	     __int64 ans=dinic(0,n-1);
	     cout<<ans%200001<<endl;
      }
      return 0;
}



 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值