HDU - 5889 Barricade (最短路+最大流/最小割)

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(N≤1000) and M(M≤10000).
The i-the line of the next M lines describes the i-th edge with three integers u,v and w where 0≤w≤1000 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

题目就是让求最短路基础上的最小割,那我们跑一遍Dijstra,把所有最短路上的边取出来,重新建图,跑一遍最大流即可。

(PS:代码太不稳了,不稳就多写几遍。)

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e3+10;
const int M = 2e4+10;
const int inf = 0x3f3f3f3f;
struct node
{
	int to,ca,next;
}g[M];
struct edge
{
	int u,v,w;
	edge(){}
	edge(int u,int v,int w):u(u),v(v),w(w){}
};
struct Node
{
	int to,dis;
	Node(){}
	Node(int to,int dis):to(to),dis(dis){}
	friend bool operator <(Node a,Node b)
	{
		return a.dis>b.dis;
	}
};
int head[N],cur[N],dis[N],deep[N],cnt,n,m;
bool vis[N];
vector<edge> ve[N];
void Init()
{
	cnt=0;
	for(int i=1;i<=n;i++)
		head[i]=-1,ve[i].clear();
	return ;
}
void add(int u,int v,int w)
{
	g[cnt].to=v;
	g[cnt].ca=w;
	g[cnt].next=head[u];
	head[u]=cnt++;
	return ;
}
//Dijstra从源点到汇点跑最短路 
void Dijs(int s,int t)
{
	memset(dis,inf,sizeof(dis));
	memset(vis,0,sizeof(vis));
	priority_queue<Node> q;
	Node now;
	int u,v;
	dis[s]=0;
	q.push(Node(s,0));
	while(!q.empty())	
	{
		now=q.top();
		q.pop();
		u=now.to;
		//cout<<u<<endl;
		//if(u==t) return;
		if(vis[u]) continue;
		vis[u]=1;
		for(int i=head[u];i!=-1;i=g[i].next)
		{
			v=g[i].to;
			if(dis[v]>dis[u]+1)
			{
				dis[v]=dis[u]+1;
				q.push(Node(v,dis[v]));
				ve[v].clear();
				ve[v].push_back(edge(u,v,g[i].ca));
			}
			else if(dis[v]==dis[u]+1)
			{
				ve[v].push_back(edge(u,v,g[i].ca));
			}
		}	
	}
	return ;
}
//用所有最短路的边建图 
void Build()
{
	cnt=0;
	for(int i=1;i<=n;i++)
		head[i]=-1;
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<ve[i].size();j++)
		{
			//cout<<ve[i][j].u<<" "<<ve[i][j].v<<endl;
			add(ve[i][j].u,ve[i][j].v,ve[i][j].w);
			add(ve[i][j].v,ve[i][j].u,0);//反向弧容量为0!!!!!!!!! 
		}
	}
	return ;
}
//bfs分层图 
bool bfs(int s,int t)
{
	memset(deep,0,sizeof(deep));
	queue<int> q;
	int u,v;
	deep[s]=1;
	q.push(s);
	while(!q.empty())
	{
		u=q.front();
		q.pop();
		//cout<<u<<endl;
		if(u==t) return 1;
		for(int i=head[u];i!=-1;i=g[i].next)
		{
			v=g[i].to;
			if(g[i].ca>0&&!deep[v])
			{
				deep[v]=deep[u]+1;
				q.push(v);
			}
		}
	}
	return deep[t]!=0;	
}
//dfs增广路 
int dfs(int u,int flow)
{
	//cout<<u<<endl;
	int v;
	if(flow==0||u==n) return flow;
	int nowflow;
	for(int& i=cur[u];i!=-1;i=g[i].next)
	{
		v=g[i].to;
		if(g[i].ca>0&&deep[v]==deep[u]+1)
		{
			nowflow=dfs(v,min(flow,g[i].ca));
			if(nowflow)
			{
				g[i].ca-=nowflow;
				g[i^1].ca+=nowflow;
				return nowflow;
			}	
		}
	
	}
	return 0;
}
//Dinic算法 
int Dinic(int s,int t)
{
	int maxflow=0,flow;
	while(bfs(s,t))
	{
		//当前弧优化 
		for(int i=1;i<=n;i++)
			cur[i]=head[i];
		//一次找多条增广路,Dinic核心 
		while(flow=dfs(s,inf))
		{
			maxflow+=flow;
		}
	}
	return maxflow;
}
int main(void)
{
	int t,u,v,w;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		Init();
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%d",&u,&v,&w);
			add(u,v,w);
			add(v,u,w);
		}
		Dijs(1,n);
		Build();
		printf("%d\n",Dinic(1,n));
	}
	
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值