HDU 6166-Senior Pan（详解！最短路+二进制优化划分集合）

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

（集合间的最短路的定义：对于其中一个集合的任一点到另一个集合中的任一点中的最短路径中最短的一条。）

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<algorithm>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define memset(a,v)  memset(a,v,sizeof(a))
#define X (sqrt(5)+1)/2.0  //Wythoff
#define Pi acos(-1)
#define e  2.718281828459045
using namespace std;
typedef long long int LL;
const int MAXL(1e5);
const int INF(0x3f3f3f3f);
const int mod(1e9+7);
int dir[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};
LL dis[MAXL+50];
int n,m,k;
int a[MAXL+50];
int haxi[MAXL+50];
bool vis[MAXL+50];
vector<pair<int,int> >v[MAXL+50];//邻接表存储图的信息
pair<int,int>p;
LL SPFA()
{
queue<int>q;
memset(vis,false);
memset(dis,INF);
for(int i=1;i<=k;i++)
{
if(haxi[i])//把标记为1的集合设为起点
{
q.push(a[i]);
dis[a[i]]=0;
vis[a[i]]=true;
}
}
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
int len=v[u].size();
for(int i=0;i<len;i++)
{
int ed=v[u][i].first;
int power=v[u][i].second;
if(dis[u]+power<dis[ed])
{
dis[ed]=dis[u]+power;
if(!vis[ed])
{
q.push(ed);
vis[ed]=true;
}
}
}
}
LL ans=INF;
for(int i=1;i<=k;i++)
{
if(!haxi[i])
ans=min(ans,dis[a[i]]);
}
return ans;
}
LL solve()
{
sort(a+1,a+k+1);
int ma=a[k];
int len=0;
while(ma)//找到k个点里最大值的二进制位数
len++,ma>>=1;
LL ans=INF;
for(int i=0;i<len;i++)
{
for(int j=1;j<=k;j++)
{
if((a[j]>>i)&1)//将当前位为1的标记为1
haxi[j]=1;
else//将当前位为0的标记为0
haxi[j]=0;
}
ans=min(ans,SPFA());//第一次SPFA
for(int j=1;j<=k;j++)
haxi[j]=!haxi[j];
ans=min(ans,SPFA());//把集合互换起点终点重新SPFA
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
int CASE=1;
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
p=make_pair(y,z);
v[x].push_back(p);//有向图
}
scanf("%d",&k);
for(int i=1;i<=k;i++)
scanf("%d",a+i);
LL ans=solve();
cout<<"Case #"<<CASE++<<": "<<ans<<endl;
for(int i=0;i<=n;i++)//每次清空邻接表
v[i].clear();
}
}