Hdu 6166 Senior Pan【思维+随机化+最短路】好题~

Senior Pan

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 81    Accepted Submission(s): 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

题目大意:


一个N个点,M条有向边的图,给出K个关键点,让我们从这K个关键点中,选择一个点作为起点,一个点作为终点,使得从起点到终点的最短距离是所有选择中最小的。问这个最短距离长度。


思路:


①我们在一个集合中找两个点显然有些难度,我们假设有一个起点集合,一个终点集合的话,我们建立一个超级源点连入起点集合的各个点,然后再建立一个超级汇点,将终点集合的各个点连入这个超级汇点的话,我们跑一遍从超级源点到超级汇点的最短路就很简单能够解决问题。


②但是我们现在没有这两个集合,我们不妨考虑将关键点集合分出两个集合,那么我们怎样分能够使得出错率最小呢?我们显然希望对半分。

所以我们随机这K个关键点的排列顺序,然后前半部分作为起点集合,后半部分作为终点集合去跑就行了。


③随机一次出来的结果出错率为3/4(正确的概率:起点分布正确的概率是1/2.终点分布正确的概率是1/2.相乘为1/4).两次都出错的概率为3/4*3/4.三次都出错的概率为3/4*3/4*3/4.依次类推,显然随机的次数越多,正确结果的概率越大。我们只需要其中任意一次正确即可。所以这样做的正确率是可以保证的。


所以我们随机20次左右就足够了,过程跑SPFA,维护最小解即可。


Ac代码:

#include
#include
#include
#include
using namespace std;
#define ll __int64
struct node
{
    ll from,to,next,w;
} e[150000];
ll cont,n,m,K,ss,tt,ans;
ll xx[150000];
ll yy[150000];
ll ww[150000];
ll ned[150000];
ll head[150000];
ll dist[150000];
ll vis[150000];
void add(ll from,ll to,ll w)
{
    e[cont].w=w;
    e[cont].to=to;
    e[cont].next=head[from];
    head[from]=cont++;
}
void SPFA()
{
    queues;
    memset(vis,0,sizeof(vis));
    for(ll i=1; i<=tt; i++)dist[i]=0x3f3f3f3f;
    dist[ss]=0;
    s.push(ss);
    while(!s.empty())
    {
        ll u=s.front();
        s.pop();
        vis[u]=0;
        for(ll i=head[u];i!=-1;i=e[i].next)
        {
            ll v=e[i].to;
            ll w=e[i].w;
            if(dist[v]>dist[u]+w)
            {
                dist[v]=dist[u]+w;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                }
            }
        }
    }
    ans=min(ans,dist[tt]);
}
void Slove()
{
    ans=0x3f3f3f3f;
    ll temp=20;
    while(temp--)
    {
        ss=n+1;
        tt=n+2;
        cont=0;
        memset(head,-1,sizeof(head));
        random_shuffle(ned,ned+K);
        for(ll i=1; i






你可能感兴趣的:(思维,最短路及其拓展)