hdu 5889 Barricade 2016ACM/ICPC青岛赛区网络赛1011



Problem Description
The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as  N  towns and  M  roads, and each road has the same length and connects two towns. The town numbered  1  is where general's castle is located, and the town numbered  N  is where the enemies are staying. The general supposes that the enemies would choose a shortest path. He knows his army is not ready to fight and he needs more time. Consequently he decides to put some barricades on some roads to slow down his enemies. Now, he asks you to find a way to set these barricades to make sure the enemies would meet at least one of them. Moreover, the barricade on the  i -th road requires  wi  units of wood. Because of lacking resources, you need to use as less wood as possible.
 

Input
The first line of input contains an integer  t , then  t  test cases follow.
For each test case, in the first line there are two integers  N(N1000)  and  M(M10000) .
The  i -the line of the next  M  lines describes the  i -th edge with three integers  u,v  and  w  where  0w1000  denoting an edge between  u  and  v  of barricade cost  w .
 

Output
For each test cases, output the minimum wood cost.
 

Sample Input
  
  
1 4 4 1 2 1 2 4 2 3 1 3 4 3 4
 

Sample Output
  
  
4

因为只会走最短路,所以不在最短路图里的边我们可以删掉

然后跑最小割即可

#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int head[100001],exhead[100001];
struct map
{
	 int f;
	 int s,t;
     int next;
}a[400001],exa[400001];
int edge,exedge;
int p; //源0汇p 
int q[400001],d[400001];
inline void add(int s,int t,int f)
{
    a[edge].next=head[s];
    head[s]=edge;
    a[edge].s=s;
    a[edge].t=t;
    a[edge].f=f;
}
inline void exadd(int s,int t,int f)
{
    exa[exedge].next=exhead[s];
    exhead[s]=exedge;
    exa[exedge].s=s;
    exa[exedge].t=t;
    exa[exedge].f=f;
}
inline bool bfs()
{
	int l=0,r=0;
    memset(q,0,sizeof(q));
    memset(d,-1,sizeof(d));
    r++; q[r]=1;
    d[1]=0;
    int i,k;
    while(l<r)
    {
   		l++;
        int k=q[l];
        for(i=head[k];i!=0;i=a[i].next)
        {
            if(a[i].f>0&&d[a[i].t]==-1)
            {
                d[a[i].t]=d[k]+1;
                r++; q[r]=a[i].t;
            }
        }
    }
    if(d[p]>=0) return true;
    return false;
}
inline int dfs(int k,int s)
{
    if(k==p) return s;
    int t=s,i;
    for(i=head[k];i!=0;i=a[i].next)
    {
        if(d[a[i].t]==d[k]+1&&a[i].f>0)
        {
            int xx=dfs(a[i].t,min(s,a[i].f));
            a[i].f-=xx;
            s-=xx;
            if(i%2==0) a[i-1].f+=xx;
            else a[i+1].f+=xx;
       }
    }
    if(t==s) d[k]=-1;
    return t-s;
}
inline int maxflow()
{
     int s=0;
     while(bfs()) s+=dfs(1,2100000000);
     return s;
}
queue<int> qq;
bool v[100001];
int dis[100001];
inline void spfa()
{
	memset(v,false,sizeof(v));
	memset(dis,127/3,sizeof(dis));
	qq.push(1);
	v[1]=true;
	dis[1]=0;
	while(!qq.empty())
	{
		int d=qq.front();
		v[d]=true;
		qq.pop();
		int i;
		for(i=exhead[d];i!=0;i=exa[i].next)
		{
			int t=exa[i].t;
			if(dis[d]+1<dis[t])
			{
				dis[t]=dis[d]+1;
				if(!v[t])
				{
					v[t]=true;
					qq.push(t);
				}
			}
		}
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T>0)
	{
		T--;
		edge=0;
		exedge=0;
		memset(head,0,sizeof(head));
		memset(exhead,0,sizeof(exhead));
		int n,m;
		scanf("%d%d",&n,&m);
		int i;
		int s,t,x;
		for(i=1;i<=m;i++)
		{
			scanf("%d%d%d",&s,&t,&x);
			exedge++;
			exadd(s,t,x);
			exedge++;
			exadd(t,s,x);
		}
		spfa();
		for(i=1;i<=exedge;i++)
		{
			s=exa[i].s;
			t=exa[i].t;
			if(dis[s]+1==dis[t])
			{
				edge++;
				add(s,t,exa[i].f);
				edge++;
				add(t,s,0);
			}
		}
		p=n;
		printf("%d\n",maxflow());
	}
	return 0;
}


Problem Description
The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as  N  towns and  M  roads, and each road has the same length and connects two towns. The town numbered  1  is where general's castle is located, and the town numbered  N  is where the enemies are staying. The general supposes that the enemies would choose a shortest path. He knows his army is not ready to fight and he needs more time. Consequently he decides to put some barricades on some roads to slow down his enemies. Now, he asks you to find a way to set these barricades to make sure the enemies would meet at least one of them. Moreover, the barricade on the  i -th road requires  wi  units of wood. Because of lacking resources, you need to use as less wood as possible.
 

Input
The first line of input contains an integer  t , then  t  test cases follow.
For each test case, in the first line there are two integers  N(N1000)  and  M(M10000) .
The  i -the line of the next  M  lines describes the  i -th edge with three integers  u,v  and  w  where  0w1000  denoting an edge between  u  and  v  of barricade cost  w .
 

Output
For each test cases, output the minimum wood cost.
 

Sample Input
   
   
1 4 4 1 2 1 2 4 2 3 1 3 4 3 4
 

Sample Output
   
   
4
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值