Path HDU6582 (最短路+最小割)

本文介绍了一种结合最短路径算法与最小割算法的解决方案,用于解决特定类型的图论问题。具体而言,该方案首先通过两次最短路径算法重构最短路径,然后应用最小割算法来确定删除哪些边可以使最短路径的长度增加,同时最小化删除边的总成本。

题面:
Years later, Jerry fell in love with a girl, and he often walks for a long time to pay visits to her. But, because he spends too much time with his girlfriend, Tom feels neglected and wants to prevent him from visiting her.
After doing some research on the neighbourhood, Tom found that the neighbourhood consists of exactly n houses, and some of them are connected with directed road. To visit his girlfriend, Jerry needs to start from his house indexed 1 and go along the shortest path to hers, indexed n.
Now Tom wants to block some of the roads so that Jerry has to walk longer to reach his girl’s home, and he found that the cost of blocking a road equals to its length. Now he wants to know the minimum total cost to make Jerry walk longer.
Note, if Jerry can’t reach his girl’s house in the very beginning, the answer is obviously zero. And you don’t need to guarantee that there still exists a way from Jerry’s house to his girl’s after blocking some edges.
题意:
给你一张有向图,现在你需要删除一些边,每次删除的花费是边的权值,使得最短路增大,现在问你最小的花费。
思路: 如果要使得最短路增大,显然是删掉最短路上的一些边。所以我们跑两次最短路算法将最短路的图重构出来,接着套模板跑一遍最小割即可
最小割==最大流

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int N=1e4+5;
struct node
{
	int v,nxt,w;
}edge2[N],edge[N*2];

bool vis[N];
int u[N],v[N],w[N],cnt,s,t;
int dep[N],cur[N],head[N],head2[N];
ll  dis1[N],dis2[N],flow[N*2];

void add2(int u,int v,int w)
{

	edge2[cnt].w=w;
	edge2[cnt].v=v;
	edge2[cnt].nxt=head2[u];
	head2[u]=cnt++;
}

void add(int u,int v,int w)
{

	edge[cnt].w=w;
	edge[cnt].v=v;
	edge[cnt].nxt=head[u];
	head[u]=cnt;
	flow[cnt++]=w;
}

void dij(int s,ll dis[])
{
	priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> > >q;
	q.push({0ll,s});
	dis[s]=0;
	while(!q.empty())
	{
		int u=q.top().second;
		q.pop();
		if(vis[u])
			continue;
		vis[u]=1;
		for(int i=head2[u];i!=-1;i=edge2[i].nxt)
		{
			int v=edge2[i].v;
			int w=edge2[i].w;
			if(dis[v]>dis[u]+w)
			{
				dis[v]=dis[u]+w;
				q.push({dis[v],v});
			}
		}
	}
}

void init(int n)
{
	for(int i=1;i<=n;i++)
	{
		head[i]=head2[i]=-1;
		dis1[i]=dis2[i]=INF;
		vis[i]=0;
		cur[i]=0;
		dep[i]=0;
	}
	cnt=0;
}

ll dfs(int u,ll cost)
{
	if(u==t)
		return cost;
	for(int i=cur[u];i!=-1;i=edge[i].nxt)
	{
		int v=edge[i].v;
		if(dep[v]==dep[u]+1&&flow[i]>0)
		{
			ll dis=dfs(v,min(flow[i],cost));
			if(dis>0)
			{
				flow[i]-=dis;
				flow[i^1]+=dis;
				return dis;
			}
		}
	}
	return 0;
}

int bfs()
{
	queue<int>q;
	q.push(1);
	memset(dep,0,sizeof dep);
	dep[1]=1;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=edge[i].nxt)
		{
			int v=edge[i].v;
			if(flow[i]>0&&dep[v]==0)
			{
				dep[v]=dep[u]+1;
				q.push(v);
			}
		}
	}
	if(dep[t]>0)
		return 1;
	return 0;
}

ll dicnic()
{
	ll ans=0;
	while(bfs())
	{
		ll cost;
		memcpy(cur,head,sizeof(head));
		while(cost=dfs(1,INF))
			ans+=cost;
	}
	return ans;
}

int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,m;
		scanf("%d%d",&n,&m);
		init(n);
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%d",&u[i],&v[i],&w[i]);
			add2(u[i],v[i],w[i]);
		}
		dij(1,dis1);
		for(int i=0;i<=n;i++)
		{
			vis[i]=0;
			head2[i]=-1;
		}
		cnt=0;
		for(int i=1;i<=m;i++)
			add2(v[i],u[i],w[i]);
		dij(n,dis2);
		cnt=0;
		t=n;
		for(int i=1;i<=m;i++)
		{
			 if(dis1[u[i]]+w[i]+dis2[v[i]]==dis1[n])
			{
				add(u[i],v[i],w[i]);
				add(v[i],u[i],0);
			}
		}
		printf("%lld\n",dicnic());
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值