题目链接
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.
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
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
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
题意:
给你一个有向图,然后给你k个点,求其中一个点到另一个点的距离的最小值
题解:
这个题很难想啊,不过是个超级棒的题目,首先将k个点划分为两个集合,可以通过二进制枚举来确保某个集合任意两点不在一个集合中,100000个点最多2^17次方,也就是划分17次
每划分一次进行一次spfa,求其中最小值。
代码:
#include <stdio.h> #include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <utility> #include <vector> #define mem(arr, num) memset(arr, 0, sizeof(arr)) #define _for(i, a, b) for (int i = a; i <= b; i++) #define __for(i, a, b) for (int i = a; i >= b; i--) #define IO \ ios::sync_with_stdio(false); \ cin.tie(); \ cout.tie(); using namespace std; typedef long long ll; const ll inf = 0x3f3f3f3f; ; const ll mod = 1000000007LL; + ; << ; int dis[N], vis[N]; int num, V, E; int po[N], belong[N]; int minn = inf; struct edge{ int point, value; edge(){} edge(int _p,int _v){point = _p, value = _v;} }; vector <edge> e[N]; void spfa() { _for(i, , V) dis[i] = inf; queue <int > q; _for(i, , num) { if(!belong[i]) { dis[po[i]] = ; q.push(po[i]); vis[po[i]] = ; } } while(!q.empty()) { int v = q.front(); q.pop(); vis[v] = ; ; i < e[v].size(); i++){ if(dis[e[v][i].point]>dis[v] + e[v][i].value) { dis[e[v][i].point] = dis[v] + e[v][i].value; if(!vis[e[v][i].point]) vis[e[v][i].point] = ,q.push(e[v][i].point); } } } _for(i, , num) if(belong[i]) minn = min(minn, dis[po[i]]); } int main() { int T; int s, ee, value; cin >> T; ; k <= T; k++) { cin >> V >> E; mem(po, ); mem(belong, ); minn = inf; _for(i, , V) e[i].clear(); _for(i, , E){ cin >> s >> ee >> value; e[s].push_back(edge(ee, value) ); } cin >> num; _for(i, , num) cin >> po[i]; sort(po+, po + num +); int len = log2(po[num]); _for(i, , len) { _for(j, , num) { <<i) ) belong[j] = ; ; } spfa(); } printf("Case #%d: %d\n",k,minn); } ; }