HDU6166 Senior Pan 解题报告【图论】【SPFA最短路】【随机】

来源:互联网 发布:21天学通java 第6版 编辑:程序博客网 时间:2024/06/05 17:15

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.
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
解题报告
这道题的题意是,给我们一张图,问图上k个给定的点两辆最短路最小的值是多少。
我们假设答案的那个点中的一个在k个点中的前k/2个点中,后一个点在k个点中的后k/2个点中,那么我们建一个超级源点,一个超级汇点,把这k个点的一头一尾连起来,那么从超级源点出发跑到超级汇点跑一个最短路就行了。
为了保证答案的正确性,我们用随机函数打乱k个点的顺序,把上述过程重复很多遍(17-30遍)就行了。
代码如下:

#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#define NAME "graph"using namespace std;const int N=100000,M=100000,inf=0x7fffffff;struct edge{    int v,w,next;}ed[M+5];int head[N+5],num;int dis[N+5],flag[N+5],p[N+5],x[N+5],y[N+5],w[N+5];int n,m,k;int ans,T,kase;void build(int u,int v,int w){    ed[++num].v=v;    ed[num].w=w;    ed[num].next=head[u];    head[u]=num;}void SPFA(int st,int end){    for(int i=1;i<=n+2;i++)dis[i]=inf;    memset(flag,0,sizeof(flag));    deque<int>q;    q.push_back(st);    flag[st]=1,dis[st]=0;    while(!q.empty())    {        int u=q.front();q.pop_front();        flag[u]=0;        for(int i=head[u];i!=-1;i=ed[i].next)        {            int v=ed[i].v;            if(dis[v]>dis[u]+ed[i].w)            {                dis[v]=dis[u]+ed[i].w;                if(!flag[v])                {                    flag[v]=1;                    if(!q.empty())                    {                        if(dis[v]>dis[q.front()])q.push_back(v);                        else q.push_front(v);                    }                    else q.push_back(v);                }            }        }    }    ans=min(ans,dis[end]);}void solve(){    ans=inf;    int temp=30;    while(temp--)    {        int st=n+1,end=n+2;        num=0;        memset(head,-1,sizeof(head));        random_shuffle(p+1,p+k+1);        for(int i=1;i<m;i++)build(x[i],y[i],w[i]);        for(int i=1;i<=k/2;i++)build(st,p[i],0);        for(int i=k/2+1;i<=k;i++)build(p[i],end,0);        SPFA(st,end);    }}int main(){    scanf("%d",&T);    while(T--)    {        scanf("%d%d%d",&n,&m,&k);        num=0;        memset(head,-1,sizeof(head));        for(int i=1;i<=m;i++)scanf("%d%d%d",&x[i],&y[i],&w[i]);        for(int i=1;i<=k;i++)scanf("%d",&p[i]);        solve();        printf("Case #%d: %d",++kase,ans);    }    return 0;}
原创粉丝点击