HDU 6166 Senior Pan dijkstra(超级源点+超级汇点)

http://acm.hdu.edu.cn/showproblem.php?pid=6166
Problem Description
Senior Pan fails in his discrete math exam again. So he asks Master ZKC to give him graph theory problems everyday.
The task is simple : ZKC will give Pan a directed graph every time, and selects some nodes from that graph, you can calculate the minimum distance of every pair of nodes chosen in these nodes and now ZKC only cares about the minimum among them. That is still too hard for poor Pan, so he asks you for help.

Input
The first line contains one integer T, represents the number of Test Cases.1≤T≤5.Then T Test Cases, for each Test Cases, the first line contains two integers n,m representing the number of nodes and the number of edges.1≤n,m≤100000
Then m lines follow. Each line contains three integers xi,yi representing an edge, and vi representing its length.1≤xi,yi≤n,1≤vi≤100000
Then one line contains one integer K, the number of nodes that Master Dong selects out.1≤K≤n
The following line contains K unique integers ai, the nodes that Master Dong selects out.1≤ai≤n,ai!=aj

Output
For every Test Case, output one integer: the answer

Sample Input
1
5 6
1 2 1
2 3 3
3 1 3
2 5 1
2 4 2
4 3 1
3
1 3 5

Sample Output
Case #1: 2

Source
2017 Multi-University Training Contest - Team 9

题目大意:给一张有向图,选中了其中的 k k k个点,求这 k k k个点中任意两点距离的最小值。

思路:看输入范围就知道暴力是不行滴。首先要知道 d i j k s t r a dijkstra dijkstra算法的一个扩展应用:给定集合 A A A和集合 B B B,求集合 A A A中的点到集合 B B B中的点的最短距离,保证集合 A A A和集合 B B B没有相交的点,那么建立超级源点 S S S和超级源点 T T T S S S A A A连权值为 0 0 0的有向边, B B B T T T连权值为 0 0 0的有向边,以 S S S为起点跑一次 d i j k s t r a dijkstra dijkstra即可得到答案。其次我们知道,对于任意两个不同的数字,它们的二进制表示中至少有 1 1 1位是不同的,根据这一点,我们可以把 k k k个点分成两个不相交的集合,然后跑一次 d i j k s t r a dijkstra dijkstra,这样只需要跑 l o g n logn logn次就可以得到答案。正确性嘛,很容易证明,通过这 l o g n logn logn次划分, [ 1 , n ] [1,n] [1,n]的任意两个数字至少有一次被分到了不同的集合,也就是说这 k k k个点中任意两个点的最短距离至少被计算了 1 1 1次,那么得到的结果肯定是正确的。因为这道题是有向图,所以每次划分要跑两次 d i j k s t r a dijkstra dijkstra。然后源点、汇点的那些边其实是没必要建出来的,具体看代码吧。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#define pr pair<long long,int>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

const int maxn=1e5+5;

int head[maxn],a[maxn];
ll dis[maxn];
bool vis[maxn],flag[maxn];
priority_queue<pr,vector<pr>,greater<pr> > q;
int n,m,k,tot;

struct Edge
{
	int to,nxt;
	ll dis;
}edge[maxn];

void init()
{
	while(!q.empty())
		q.pop();
	for(int i=1;i<=n;i++)
		dis[i]=1e16,vis[i]=flag[i]=0;
}

void addedge(int u,int v,int dis)
{
	edge[++tot].to=v,edge[tot].nxt=head[u],edge[tot].dis=dis,head[u]=tot;
}

ll dijkstra()
{
	pr p;
	while(!q.empty())
	{
		p=q.top();
		q.pop();
		if(vis[p.second])
			continue;
		if(flag[p.second])//到汇点中距离最小的那个
			return p.first;
		vis[p.second]=1;
		for(int i=head[p.second];i!=-1;i=edge[i].nxt)
		{
			if(!vis[edge[i].to]&&dis[edge[i].to]>dis[p.second]+edge[i].dis)
			{
				dis[edge[i].to]=dis[p.second]+edge[i].dis;
				q.push(pr(dis[edge[i].to],edge[i].to));
			}
		}
	}
	return 1e16;
}

int main()
{
	int t,times=0;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		memset(head,-1,sizeof(head));
		tot=0;
		int u,v,dist;
		for(int i=0;i<m;i++)
		{
			scanf("%d%d%d",&u,&v,&dist);
			addedge(u,v,dist);
		}
		scanf("%d",&k);
		for(int i=0;i<k;i++)
			scanf("%d",&a[i]);
		ll ans=1e16;
		int MAX=log2(n)+1;
		for(int i=0;i<MAX;i++)
		{
			init();
			for(int j=0;j<k;j++)
			{
				if(a[j]&(1<<i))//超级源点
					q.push(pr(0,a[j])),dis[a[j]]=0;
				else
					flag[a[j]]=1; //超级汇点
			}
			ans=min(ans,dijkstra());
			init(); //有向图 反着再求一次
			for(int j=0;j<k;j++)
			{
				if(a[j]&(1<<i))//超级汇点
					flag[a[j]]=1;
				else
					q.push(pr(0,a[j])),dis[a[j]]=0; //超级源点
			}
			ans=min(ans,dijkstra());
		}
		printf("Case #%d: %lld\n",++times,ans);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值