Hdu 6166 Senior Pan 随机+SPFA 解题报告

来源:互联网 发布:仿淘宝网站源码 编辑:程序博客网 时间:2024/05/16 09:49

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.依次类推,显然随机的次数越多,正确结果的概率越大。我们只需要其中任意一次正确即可。所以这样做的正确率是可以保证的。

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

代码

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#include<vector>using namespace std;const int N=200000+5;  const int inf=0x7fffffff;struct node  {      int u,v,w;      int next;};node ed[5*N];  int num=0,n,m,K,start,end;long long ans; long long x[N],y[N],w[N],vis[N],point[N],head[N],dis[N];int read(){    int x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}int min(int x,int y){    if (x<=y) return x;    else return y;}  void build(int u,int v,int w)  {      ed[num].w=w;      ed[num].v=v;      ed[num].next=head[u];      head[u]=num++;  }  void SPFA()  {      queue<int> s;      memset(vis,0,sizeof(vis));      for (int i=1;i<=end;i++)    dis[i]=inf;      dis[start]=0;      s.push(start);      while(!s.empty())      {          int u=s.front();          s.pop();          vis[u]=0;          for (int i=head[u];i!=-1;i=ed[i].next)          {              int v=ed[i].v,w=ed[i].w;              if (dis[v]>dis[u]+w)              {                  dis[v]=dis[u]+w;                  if (vis[v]==0)                  {                      vis[v]=1;                      s.push(v);                  }              }          }      }      ans=min(ans,dis[end]);  }  void Solve()  {      ans=inf;      int temp=15;      while(temp--)      {          start=n+1;end=n+2;num=0;          memset(head,-1,sizeof(head));          random_shuffle(point,point+K);          for (int i=1;i<m;i++)          build(x[i],y[i],w[i]);          for (int i=0;i<K/2;i++)          build(start,point[i],0);          for (int i=K/2;i<K;i++)          build(point[i],end,0);          SPFA();      }      if (ans!=0) printf("%I64d\n",ans);    else printf("10\n");  }  int main()  {      freopen("graph.in","r",stdin);    freopen("graph.out","w",stdout);    int T,kase=0;    T=read();    while(T--)    {        n=read();m=read();K=read();        memset(head,-1,sizeof(head));          for (int i=1;i<=m;i++)         {x[i]=read();y[i]=read();w[i]=read();}         for (int i=0;i<K;i++)        scanf("%I64d",&point[i]);          printf("Case #%d: ",++kase);        Solve();      }    return 0;}  
原创粉丝点击