hdu 6166 Senior Pan 最短路


Senior Pan

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 179    Accepted Submission(s): 45


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
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6170  6169  6168  6167  6166 
 

Statistic |  Submit |  Discuss |  Note
题意:给你一个有向图,然后给你k个点,求其中一个点到另一个点的距离的最小值。

思路:恩。。。官方题解说的有点不清楚,搞得我斟酌了好一会大概知道他想说什么了。。首先一个集合到另一个集合的最短路径是可以一遍dijkstra算出来的。这个如果想不通最短路没过关。然后任意两个点在二进制表示上肯定至少有一位是不相同的,所以只要枚举二进制的位数,把k个点分成两个集合,跑logn次dijkstra就能把全部情况跑出来。。大概就这样,想通了的话代码是很好写的。下面给代码:

#pragma comment(linker, "/STACK:102400000,102400000") 
#include<iostream>  
#include<cmath>  
#include<queue>  
#include<cstdio>  
#include<queue>  
#include<algorithm>  
#include<cstring>  
#include<string>  
#include<utility>
#include<set>
#include<map>
#include<stack>
#include<vector>
#define maxn 100005
#define inf  0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long LL;
const double eps = 1e-5;
const int mod = 1e9+7;
struct Edge{
	int v, val,next;
}p[maxn];
struct node{
	int x;
	LL step;
};
int head[maxn], len, a[maxn], mark[maxn];
LL vis[maxn];
priority_queue<node>q;
bool operator<(node a, node b){
	return a.step > b.step;
}
void addedge(int u, int v,int val){
	p[len].v = v;
	p[len].val = val;
	p[len].next = head[u];
	head[u] = len++;
}
LL dijkstra(){
	while (!q.empty()){
		node now = q.top();
		q.pop();
		if (mark[now.x])
			return now.step;
		for (int i = head[now.x]; ~i; i = p[i].next){
			int next = p[i].v;
			if (vis[next] > now.step + p[i].val){
				vis[next] = now.step + p[i].val;
				q.push(node{ next, vis[next] });
			}
		}
	}
	return inf;
}
void init(){
	memset(vis, inf, sizeof(vis));
	memset(mark, 0, sizeof(mark));
	while (!q.empty())
		q.pop();
}
LL solve(int k){
	LL ans = inf;
	for (int i = 0; i < 20; i++){
		init();
		for (int j = 0; j < k; j++){
			if (a[j] & (1 << i)){
				q.push(node{ a[j], 0 });
				vis[a[j]] = 0;
			}
			else
				mark[a[j]] = 1;
		}
		ans = min(ans, dijkstra());
		init();
		for (int j = 0; j < k; j++){
			if (a[j] & (1 << i))
				mark[a[j]] = 1;
			else{
				q.push(node{ a[j], 0 });
				vis[a[j]] = 0;
			}	
		}
		ans = min(ans, dijkstra());
	}
	return ans;
}
int main(){
	int t;
	scanf("%d", &t);
	for (int tcase = 1; tcase <= t;tcase++){
		memset(head, -1, sizeof(head));
		len = 0;
		int n, m;
		scanf("%d%d", &n, &m);
		while (m--){
			int u, v, w;
			scanf("%d%d%d", &u, &v, &w);
			addedge(u, v, w);
		}
		int k;
		scanf("%d", &k);
		for (int i = 0; i < k; i++)
			scanf("%d", &a[i]);
		printf("Case #%d: %lld\n", tcase, solve(k));
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值