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
利用二进制分集合,然后设立一个超级起始点和一个超级终止点,超级起始点和所有认为是起点的集合的距离设置为0,超级终止点同理。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=100000+110;
int xx[maxn],yy[maxn],zz[maxn];
int kk[maxn],head[maxn],cnt;
struct edge
{
int v,nxt,w;
}edge[maxn*3+100];
void add_edge(int u,int v,int w)
{
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].nxt=head[u];
head[u]=cnt++;
}
struct node
{
int point,distance;
node(){}
node(int _point,int _distance)
{
point=_point;
distance=_distance;
}
friend bool operator < (node aa,node bb)
{
return aa.distance>bb.distance;
}
};
int dis[maxn],vis[maxn];
int minn=0x3f3f3f3f;
int n,m;
void zyz(int x,int y)
{
for(int i=0;i<=n+1;i++)
{
dis[i]=0x3f3f3f3f;
}
memset(vis,0,sizeof(vis));
dis[x]=0;
priority_queue<node>q;
q.push(node(x,dis[x]));
while(!q.empty())
{
node now=q.top();
q.pop();
if(vis[now.point])
continue;
vis[now.point]=1;
for(int i=head[now.point];i!=-1;i=edge[i].nxt)
{
int v=edge[i].v;
if(vis[v]==0&&dis[v]>dis[now.point]+edge[i].w)
{
dis[v]=dis[now.point]+edge[i].w;
q.push(node(v,dis[v]));
}
}
}
/* for(int i=1;i<=n+1;i++)
{
printf(" %d ",dis[i]);
}
printf("\n");*/
minn=min(minn,dis[y]);
}
int main ()
{
int t,icase=0;
scanf("%d",&t);
while(t--)
{
++icase;
minn=0x3f3f3f3f;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&xx[i],&yy[i],&zz[i]);
}
int k;
scanf("%d",&k);
for(int i=1;i<=k;i++)
{
scanf("%d",&kk[i]);
}
for(int i=0;i<20;i++)
{
memset(head,-1,sizeof(head));
cnt=0;
for(int j=1;j<=m;j++)
{
add_edge(xx[j],yy[j],zz[j]);
}
for(int j=1;j<=k;j++)
{
if(kk[j]&(1<<i))
{
//printf("kk %d ",kk[j]);
add_edge(0,kk[j],0);
}
else
{
add_edge(kk[j],n+1,0);
}
}
//printf("\n");
{
//cout<<"111"<<endl;
zyz(0,n+1);
}
memset(head,-1,sizeof(head));
cnt=0;
for(int j=1;j<=m;j++)
{
add_edge(xx[j],yy[j],zz[j]);
}
for(int j=1;j<=k;j++)
{
if((kk[j]&(1<<i))==0)
{
//printf("kk %d ",kk[j]);
add_edge(0,kk[j],0);
}
else
{
add_edge(kk[j],n+1,0);
}
}
{
//cout<<"111"<<endl;
zyz(0,n+1);
}
}
printf("Case #%d: %d\n",icase,minn);
}
}