HDU6166-Senior Pan

来源:互联网 发布:mac sudo chown r 编辑:程序博客网 时间:2024/06/08 04:31

Senior Pan

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


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
15 61 2 12 3 33 1 32 5 12 4 24 3 131 3 5
 

Sample Output
Case #1: 2
 

Source
2017 Multi-University Training Contest - Team 9
 

Recommend
liuyiding
 


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

解题思路:首先一个集合到另一个集合的最短路径是可以一遍dijkstra算出来的。然后任意两个点在二进制表示上肯定至少有一位是不相同的,所以只要枚举二进制的位数,把k个点分成两个集合,跑logn次Dijkstra就能把全部情况跑出来



#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f3f3f3f3f;int s[100009], nt[100009], e[100009];int n, m, cnt, k, a[100009], vis[100009], mark[1000009];LL dis[100009], v[100009];struct node{int id;LL x;bool operator<(const node &a)const{return x > a.x;}}pre;priority_queue<node>q;void init(){while (!q.empty()) q.pop();memset(vis, 0, sizeof vis);memset(dis, INF, sizeof dis);memset(mark, 0, sizeof mark);}LL Dijkstra(){while (!q.empty()){pre = q.top();q.pop();vis[pre.id] = 1;if (mark[pre.id]) return pre.x;for (int i = s[pre.id]; ~i; i = nt[i]){if (vis[e[i]]) continue;if (dis[e[i]] > pre.x + v[i]){dis[e[i]] = pre.x + v[i];q.push(node{ e[i], dis[e[i]] });}}}return INF;}LL solve(){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 }), dis[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 }), dis[a[j]] = 0;}ans = min(ans, Dijkstra());}return ans;}int main(){int t,cas=0;scanf("%d", &t);while(t--){memset(s, -1, sizeof s);cnt = 0;scanf("%d%d", &n, &m);while (m--){int uu, vv;LL ww;scanf("%d%d%lld", &uu, &vv, &ww);nt[cnt] = s[uu], s[uu] = cnt, e[cnt] = vv, v[cnt++] = ww;}scanf("%d", &k);for (int i = 0; i < k; i++) scanf("%d", &a[i]);printf("Case #%d: %lld\n", ++cas, solve());}}

原创粉丝点击