hdu 5521 Meeting

题意:n个点,m个集合,给出长度s,集合大小,和集合里面的地点,集合中的地点两两的距离为s,问一人在1,一人在n,他们同时出发,问到达哪个点的时间最短,并且把每个地点输出

思路: 来自:http://blog.csdn.net/loy_184548/article/details/50569805

但是单纯/2会变成double,所以把距离*2,最后/2

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long ll;
  4 const int N=3200010;
  5 const ll INF=1e18;
  6 
  7 int n,m;
  8 ll dis[N],dis1[N];
  9 int head[N],len;
 10 bool vis[N];
 11 
 12 
 13 struct edge
 14 {
 15     int to,next;
 16     ll val;
 17 } e[N];
 18 
 19 void add(int from,int to,ll val)
 20 {
 21     e[len].to=to;
 22     e[len].val=val;
 23     e[len].next=head[from];
 24     head[from]=len++;
 25 }
 26 
 27 struct point
 28 {
 29     ll val;
 30     int id;
 31     point(int id,ll val):id(id),val(val) {}
 32     bool operator <(const point &x)const
 33     {
 34         return val>x.val;
 35     }
 36 };
 37 
 38 void dijkstra(int s)
 39 {
 40     memset(vis,0,sizeof(vis));
 41     for(int i=0; i<N; i++)
 42         dis[i]=INF;
 43     priority_queue<point> q;
 44     while(!q.empty()) q.pop();
 45     q.push(point(s,0));
 46     dis[s]=0;
 47     while(!q.empty())
 48     {
 49         int cur=q.top().id;
 50         q.pop();
 51         if(vis[cur]) continue;
 52         vis[cur]=true;
 53         for(int i=head[cur]; i!=-1; i=e[i].next)
 54         {
 55             int id=e[i].to;
 56             if(!vis[id] && dis[cur]+e[i].val < dis[id])
 57             {
 58                 dis[id]=dis[cur]+e[i].val;
 59                 q.push(point(id,dis[id]));
 60             }
 61         }
 62     }
 63 }
 64 
 65 vector<int > ss;
 66 
 67 int main()
 68 {
 69     int k=1;
 70     int t;
 71     cin>>t;
 72     while(t--)
 73     {
 74         scanf("%d%d",&n,&m);
 75         int pos=n;
 76         memset(head,-1,sizeof(head));
 77         len=0;
 78         ll val;
 79         for(int i=1; i<=m; i++)
 80         {
 81             scanf("%lld",&val);
 82             pos++;
 83             int x,u;
 84             scanf("%d",&x);
 85             while(x--)
 86             {
 87                 scanf("%d",&u);
 88                 add(u,pos,val);
 89                 add(pos,u,val);
 90             }
 91         }
 92         dijkstra(1);
 93         for(int i=1; i<=n; i++)
 94         {
 95             dis1[i]=dis[i];
 96         }
 97         dijkstra(n);
 98         ll Min=INF;
 99         for(int i=1; i<=n; i++)
100         {
101             Min=min(Min,max(dis[i],dis1[i]));
102             // cout<<dis[i]<<" "<<dis1[i]<<endl;
103         }
104         if(Min==INF) printf("Case #%d: Evil John\n",k++);
105         else
106         {
107             ss.clear();
108             printf("Case #%d: %lld\n",k++,Min/2);
109             for(int i=1; i<=n; i++)
110             {
111                 if(Min==max(dis[i],dis1[i]))
112                 {
113                     ss.push_back(i);
114                 }
115             }
116             printf("%d",ss[0]);
117             for(int i=1; i<ss.size(); i++)
118                 printf(" %d",ss[i]);
119             printf("\n");
120         }
121     }
122     return 0;
123 }

 

posted on 2017-09-04 21:45  hhhhx  阅读(97)  评论(0编辑  收藏  举报

导航