Senior Pan HDU

来源:互联网 发布:demo设计软件 编辑:程序博客网 时间:2024/06/15 18:06

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,yixi,yi representing an edge, and vivi representing its length.1≤xi,yixi,yi≤n,1≤vivi≤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 aiai, the nodes that Master Dong selects out.1≤aiai≤n,aiai!=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

这道题让我学习到了Dijkstra算法的另一个用法,真的很巧妙,关于求点集与点集之间的最短路,实际只要在原始的dis数组把起始的起点设为都0,就只有这个变化

#include<cstdio>#include<iostream>#include<algorithm>#include<cstdlib>#include<cstring>#include<cmath>#include<vector>#include<queue>#define INF 0x7fffffff#define N 100005using namespace std;vector<int>graph[N];int n,m,k,num;int belong[N];int some[N];int ans;struct node{    int to,w;}edge[N];void addEdge(int x,int y,int w){    edge[k].to=y;    edge[k].w=w;    graph[x].push_back(k);    k++;}void init(int x){    for(int i=1;i<=n;i++)        graph[i].clear();    k=0;    ans=INF;}int dis[N];bool inQ[N];queue<int>que;void spfa(){    for(int i=1;i<=n;i++)        dis[i]=INF;    for(int i=0;i<num;i++)    {        if(!belong[i])//把所有划到为0的集合作为源点,设其dis为0        {            dis[some[i]]=0;            que.push(some[i]);            inQ[some[i]]=true;        }    }    while(!que.empty())    {        int u=que.front();        que.pop();        inQ[u]=false;        for(int i=0;i<graph[u].size();i++)        {            int v=edge[graph[u][i]].to;            int w=edge[graph[u][i]].w;            if(dis[v]>dis[u]+w)            {                dis[v]=dis[u]+w;                if(!inQ[v])                {                    inQ[v]=true;                    que.push(v);                }            }        }    }    for(int i=0;i<num;i++)    {        if(belong[i])//终点当然是为1的点            ans=min(ans,dis[some[i]]);//然后更新答案即可    }}int main(){    int t;    int x,y,w;    scanf("%d",&t);    int ca=1;    while(t--)    {        scanf("%d%d",&n,&m);        init(n);        while(m--)        {            scanf("%d%d%d",&x,&y,&w);            addEdge(x,y,w);//建边        }        scanf("%d",&num);        int maxx=-1;        for(int i=0;i<num;i++)        {            scanf("%d",some+i);            maxx=max(maxx,some[i]);        }        int len=0;        while(maxx)        {            len++;            maxx>>=1;        }        for(int i=0;i<len;i++)        {            for(int j=0;j<num;j++)            {                if((some[j]>>i)&1)//二进制划分两个集合,可以保证点与点之间在某次划分后一定处于不同的集合                    belong[j]=1;                else                    belong[j]=0;            }            spfa();        }        printf("Case #%d: %d\n",ca++,ans);    }    return 0;}