题意:
给你一个有向图, 告诉你选择的K个点, 问这K个点中最近点对 的最短路是多少?
思路:
大开脑洞一个题目。。。
假设我们能够把K个点分成两个集合的话, 我们一遍Dijkstra 就可以求出两个集合的最近距离。
求完这两个集合后, 那么两个集合中除了最近点对外, 其余点对都不用在算了, 因为都不如这个最近点对最优。
因此 如和合理的划分集合是关键。
因为每一个点的数值都是不同的, 因此任意两个点, 二进制中至少有一位是不同的, 如果按照二进制是1还是0来划分的话, 那么就可以保证
任意两个点 都可以划分到不同集合中, 这样, 任意两个点都可以被计算到。(这里好好理解一下还是挺好想的)
10w以内的数 二进制20位就够了。
因此我们可以20次集合划分, 20次dijkstra 即可。
真的大开脑洞。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 100000 + 10;
vector<pair<int,int> >g[maxn];
int a[maxn];
vector<int>one, zero;
int vis[maxn];
long long d[maxn];
struct node{
int u, d;
bool operator < (const node& rhs) const {
return d > rhs.d;
}
node(int u = 0,int d = 0):u(u), d(d){}
};
priority_queue<node>q;
int goal[maxn];
long long bfs(vector<int>& one){
while(!q.empty()) q.pop();
memset(vis,0,sizeof vis);
memset(d,-1,sizeof d);
for (int i = 0; i < one.size(); ++i){
int v = one[i];
d[v] = 0;
q.push(node(v, 0));
}
while(!q.empty()){
node nod = q.top(); q.pop();
int u = nod.u;
if (goal[u]) return nod.d;
if (vis[u]) continue;
vis[u] = 1;
for (int i = 0; i < g[u].size(); ++i){
int v = g[u][i].first;
int w = g[u][i].second;
if (!vis[v] && (d[v] == -1 || d[v] > d[u] + w)){
d[v] = d[u] + w;
q.push(node(v, d[v]));
}
}
}
return (long long)2333333333333;
}
int main(){
int T, ks = 0;
scanf("%d",&T);
while(T--){
int n, m;
scanf("%d %d",&n, &m);
for (int i = 1; i <= n; ++i){
g[i].clear();
}
for (int i = 0; i < m; ++i){
int x, y, w;
scanf("%d %d %d",&x, &y, &w);
g[x].push_back(make_pair(y, w));
}
int K;
scanf("%d", &K);
for (int i = 0; i < K; ++i){
scanf("%d", &a[i]);
}
long long ans = (long long)1e18;
for (int i = 0; i < 20; ++i){
one.clear();
zero.clear();
for (int j = 0; j < K; ++j){
goal[a[j] ] = 0;
}
for (int j = 0; j < K; ++j){
int v = a[j];
if (v & (1<<i)){
one.push_back(v);
}
else {
zero.push_back(v);
goal[v] = 1;
}
}
ans = min(ans, bfs(one));
for (int j = 0; j < K; ++j){
goal[a[j] ] = 0;
}
for (int i = 0; i < one.size(); ++i){
goal[one[i] ] = 1;
}
ans = min(ans, bfs(zero));
}
printf("Case #%d: %I64d\n", ++ks, ans);
}
return 0;
}
Senior Pan
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 243 Accepted Submission(s): 70
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
Sample Output
Case #1: 2
Source
Recommend