HDU 6166 最短路+随机

Senior Pan

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个关键点的排列顺序,然后前半部分作为起点集合,后半部分作为终点集合去跑就行了。
随机一次出来的结果出错率为3/4(正确的概率:起点分布正确的概率是1/2.终点分布正确的概率是1/2.相乘为1/4).两次都出错的概率为3/4*3/4.三次都出错的概率为3/4*3/4*3/4.依次类推,显然随机的次数越多,正确结果的概率越大。我们只需要其中任意一次正确即可。所以这样做的正确率是可以保证的。
所以我们随机20次左右就足够了,过程跑SPFA,维护最小解即可。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<ctime>
using namespace std;
#define N 1000010
#define M 3000010
#define inf 0x3f3f3f3f

inline int read()
{
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}

int ans;
int n,m,k;
struct Edge{int to,nxt,w;}e[M];
int cnt,head[N],vis[N],dis[N];
int node[N];
int xx[N],yy[N],ww[N];
int ss,tt;

void adde(int u,int v,int w)
{
    e[++cnt].w=w;
    e[cnt].to=v;
    e[cnt].nxt=head[u];
    head[u]=cnt;
}
void SPFA()
{
    memset(vis,0,sizeof(vis));
    memset(dis,inf,sizeof(dis));
    deque<int> q;
    dis[ss]=0,vis[ss]=1;
    q.push_back(ss);
    while(!q.empty())
    {
        int now=q.front();q.pop_front();
        vis[now]=0;
        for(int i=head[now];~i;i=e[i].nxt)
        {
            int v=e[i].to;
            if(dis[v]>dis[now]+e[i].w)
            {
                dis[v]=dis[now]+e[i].w;
                if(!vis[v])
                {
                    vis[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[tt]);
}
void Slove()  
{  
    ans=inf;  
    int temp=20;  
    while(temp--)  
    {  
        ss=n+1;tt=n+2;cnt=0;
        memset(head,-1,sizeof(head));
        random_shuffle(node+1,node+k+1);  
        for(int i=1;i<m;++i) adde(xx[i],yy[i],ww[i]);  
        for(int i=1;i<=k/2;++i) adde(ss,node[i],0);    
        for(int i=k/2+1;i<=k;++i) adde(node[i],tt,0);    
        SPFA();  
    }   
}  
int main()
{
    int t;scanf("%d",&t);  
    for(int tcase=1;tcase<=t;++tcase)
    {  
        n=read();m=read();
        for(int i=1;i<=m;++i)
        {
            xx[i]=read();yy[i]=read();ww[i]=read();
        }
        k=read();
        for(int i=1;i<=k;++i)
        {
            node[i]=read();
        }
        printf("Case #%d: ",tcase);
        Slove();
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值