Senior Pan(最短路+二进制分组)

31 篇文章 0 订阅

题目链接

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

题目大意

有一个 n 个点 m 条边的有向带边权图。现在给你 k 个点,问 k 个点中最近的点对距离。

分析

可以先将这 k 个点用二进制来进行分组,点 i 和点 j 的下标分别是 i 和 j,由于 i 不等于 j,所以 i 和 j 的二进制表达式中,一定有一位不同。那么,我们只要划分 log n 次,每次依据下标的二进制中第 i 位是 0 还是 1,分成两个集合,然后再跑最短路就可以了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
ll t, n, m, k, ans;
ll a[maxn], dis[maxn], vis[maxn], flag[maxn];

struct node{
	ll v, len;
};

vector<node> f[maxn << 1];

ll dijkstra()
{
	memset(dis, inf, sizeof(dis));
	memset(vis, 0, sizeof(vis));
	priority_queue<pii, vector<pii>, greater<pii> > q;
	for(int i = 1; i <= n; i++){
		if(flag[i] == 1){
			q.push({0, i});
			dis[i] = 0;
		}
	}
	while(!q.empty()){
		pii p = q.top();
		q.pop();
		int u = p.second;
		if(flag[u] == 2)
			return p.first;
		if(vis[u])
			continue;
		vis[u] = 1;
		for(int i = 0; i < f[u].size(); i++){
			node t = f[u][i];
			if(dis[t.v] > dis[u] + t.len){
				dis[t.v] = dis[u] + t.len;
				q.push({dis[t.v], t.v});
			}
		}
	}
	return inf;
}

void solve()	// 二进制分组 
{
	memset(flag, 0,sizeof(flag));
	ans = inf;
	for(int i = 1; i <= 17; i++){
		int bit = (1 << (i - 1));
		for(int j = 1; j <= k; j++){
			if(a[j] & bit)
				flag[a[j]] = 1;
			else
				flag[a[j]] = 2;
		}
		ans = min(ans, dijkstra());
		for(int j = 1; j <= n; j++){
			if(flag[j] == 1)
				flag[j] = 2;
			else if(flag[j] == 2)
				flag[j] = 1;
		}
		ans = min(ans, dijkstra());
	}
}

int main()
{
	cin >> t;
	for(int i = 1; i <= t; i++){
		cin >> n >> m;
		for(int j = 0; j <= n; j++)
			f[j].clear();
		for(int j = 0; j < m; j++){
			int x, y, z;
			cin >> x >> y >> z;
			f[x].push_back({y, z});
		}
		cin >> k;
		for(int j = 1; j <= k; j++)
			cin >> a[j];
		solve();
		printf("Case #%d: %lld\n", i, ans);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值