1. 程式人生 > >HDU - 6166:Senior Pan(頂點集合最短路&二進位制分組)

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.

InputThe 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">,i  xi,yi representing an edge, and i  vi representing its length.1≤,i  xi,yi ≤n,1≤i  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">i  ai , the nodes that Master Dong selects out.1≤i  ai ≤n,i  ai !=aj
OutputFor every Test Case, output one integer: the answerSample 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

題意:給定有向圖,然後給一個集合,讓你在這個集合選一個作為起點,一個作為終點,其最短路最小。

思路:上次做過樹上最大收益(邊權為花費,頂點有價格,求最大差價),每個點連線匯點,權值為負價;每個點連線源點,權值為正價。然後跑最長路。

此題也是一樣的套路,但是起點和終點不能是同一點,所以我們需要分組。

 

這裡分組可以隨機20次;也可以用“二進位制分組法”。:

 

舉例說明,將1 2 3 4 進行分組,四個數的二進位制形式為: 001 010 011 100
第一次看第0位為1的數,那麼A={1,3},B={2,4}
第一次看第1位為1的數,那麼A={2,3},B={1,4}
第一次看第2位為1的數,那麼A={4},B={1,2,3}
可以看出,任意一對數字,都至少有一次機會不在同一組中。

由於是有向圖,還得反過來來一次。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
using namespace std;
const ll inf=1234567645893456783;
const int maxn=100010;
int S,T,Laxt[maxn],Next[maxn];
int in[maxn],To[maxn],Len[maxn],a[maxn],cnt;
ll dis[maxn],ans;
vector<int>G[maxn];
void add(int u,int v,int w){
    Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; Len[cnt]=w;
}
void SPFA()
{
    rep(i,S,T) dis[i]=inf,in[i]=0;
    dis[S]=0; queue<int>q;
    q.push(S); in[S]=1;
    while(!q.empty()){
        int u=q.front(); q.pop();
        for(int i=Laxt[u];i;i=Next[i]){
            int v=To[i]; if(dis[u]+Len[i]<dis[v]) {
                dis[v]=dis[u]+Len[i];
                if(!in[v]) in[v]=1,q.push(v);
            }
        }
        for(int i=0;i<G[u].size();i++){
            int v=G[u][i]; if(dis[u]<dis[v]) {
                dis[v]=dis[u];
                if(!in[v]) in[v]=1,q.push(v);
            }
        }
        in[u]=0;
    }
    ans=min(ans,dis[T]);
}
int main()
{
    int C,Cas=0,N,M,tot,u,v,w;
    scanf("%d",&C);
    while(C--){
        scanf("%d%d",&N,&M); T=N+1; ans=inf;
        rep(j,S,T) Laxt[j]=0; cnt=0;
        rep(i,1,M) scanf("%d%d%d",&u,&v,&w),add(u,v,w);
        scanf("%d",&tot);
        rep(i,1,tot) scanf("%d",&a[i]);
        rep(i,0,17){
            rep(j,S,T) G[j].clear();
            rep(j,1,tot)
              if(j&(1<<i)) G[S].push_back(a[j]);
              else G[a[j]].push_back(T);
            SPFA();
            rep(j,S,T) G[j].clear();
            rep(j,1,tot)
              if(j&(1<<i)) G[a[j]].push_back(T);
              else G[S].push_back(a[j]);
            SPFA();
        }
        printf("Case #%d: %lld\n",++Cas,ans);
    }
    return 0;
}