Senior Pan
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1462 Accepted Submission(s): 573
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
给出N个点和M条边的无向图,从中选出K个互不相同的点组成集合D,问从D中挑选任意两点间的最短路最小是多少。
如果让每个点都跑一次dij的话肯定会T。
首先我们要知道对于求两个集合之间的最短路只要对每个集合建立一个超级点跑一次dij便可得到。可以想办法把这个集合划分成若两个个小集合然后减少dij运行次数,要满足所有的点对都会分在两个不同的集合中。利用二进制,两个点的编号都不相同,那么对于她们的二进制而言一定是有一位是不同的,点数最大为1e5,最多也就16位足以,这样每次根据第b位为0/1进行集合划分,最后就考虑了所有情况。第一发我让超级点建了双向边(为了省事)结果T了,后来每次都重新建边就3s过了。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #include<cmath> 6 #include<algorithm> 7 #include<map> 8 #include<set> 9 #include<vector> 10 #include<functional> 11 using namespace std; 12 #define LL long long 13 #define pii pair<int,int> 14 #define mp make_pair 15 #define inf 0x3f3f3f3f 16 int u[101000],v[101000],w[101000]; 17 struct Edge{ 18 int v,w,next; 19 Edge(){} 20 Edge(int v,int w,int next):v(v),w(w),next(next){} 21 }e[322050]; 22 int first[101000],tot; 23 void add(int u,int v,int w){ 24 e[tot]=Edge(v,w,first[u]); 25 first[u]=tot++; 26 } 27 bool vis[101000]; 28 int d[101000]; 29 int N,M,K; 30 void dij(int s){ 31 memset(vis,0,sizeof(vis)); 32 memset(d,inf,sizeof(d)); 33 d[s]=0; 34 priority_queue<pii,vector<pii>,greater<pii> >q; 35 q.push(mp(0,s)); 36 while(!q.empty()){ 37 int u=q.top().second; 38 q.pop(); 39 if(vis[u]) continue; 40 vis[u]=1; 41 for(int i=first[u];i+1;i=e[i].next){ 42 if(d[e[i].v]>d[u]+e[i].w){ 43 d[e[i].v]=d[u]+e[i].w; 44 q.push(mp(d[e[i].v],e[i].v)); 45 } 46 } 47 } 48 } 49 int main() 50 { 51 int t,i,a,j; 52 int cas=0; 53 cin>>t; 54 while(t--){ 55 vector<int>vi; 56 cin>>N>>M; 57 tot=0; 58 for(i=1;i<=M;++i){ 59 scanf("%d%d%d",u+i,v+i,w+i); 60 } 61 scanf("%d",&K); 62 for(i=1;i<=K;++i){ 63 scanf("%d",&a); 64 vi.push_back(a); 65 } 66 printf("Case #%d: ",++cas); 67 int ans=inf; 68 for(int b=0;b<16;++b){ 69 tot=0; 70 memset(first,-1,sizeof(first)); 71 for(i=1;i<=M;++i) add(u[i],v[i],w[i]); 72 for(i=0;i<vi.size();++i){ 73 if((vi[i]&(1<<b))==0){ 74 add(0,vi[i],0); 75 } 76 else{ 77 add(vi[i],N+1,0); 78 } 79 } 80 dij(0); 81 ans=min(ans,d[N+1]); 82 83 tot=0; 84 memset(first,-1,sizeof(first)); 85 for(i=1;i<=M;++i) add(u[i],v[i],w[i]); 86 for(i=0;i<vi.size();++i){ 87 if((vi[i]&(1<<b))==0){ 88 add(vi[i],0,0); 89 } 90 else{ 91 add(N+1,vi[i],0); 92 } 93 } 94 95 dij(N+1); 96 ans=min(ans,d[0]); 97 } 98 cout<<ans<<endl; 99 } 100 return 0; 101 }