HDU Meeting

 1 #include <stdio.h>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <queue>
 6 using namespace std;
 7 const int maxn = 2000010;
 8 using LL = long long;
 9 using PLI = pair<LL,int>;
10 const LL INF = 0x3f3f3f3f3f3f3f3f;
11 struct arc {
12     int to,next;
13     LL w;
14     arc(int x = 0,LL y = 0,int z = -1) {
15         to = x;
16         w = y;
17         next = z;
18     }
19 } e[8000010];
20 int head[maxn],n,m,tot;
21 vector<int>belong[100010];
22 void add(int u,int v,LL w) {
23     e[tot] = arc(v,w,head[u]);
24     head[u] = tot++;
25 }
26 priority_queue<PLI,vector<PLI>,greater<PLI>>q;
27 bool done[maxn];
28 LL d[2][maxn];
29 void dijkstra(LL d[maxn],int s) {
30     while(!q.empty()) q.pop();
31     q.push(PLI(0,s));
32     d[s] = 0;
33     for(int i = belong[s].size()-1; i >= 0; --i) {
34         d[belong[s][i] + n] = 0;
35         q.push(PLI(0,belong[s][i] + n));
36     }
37     memset(done,false,sizeof done);
38     while(!q.empty()) {
39         int u = q.top().second;
40         q.pop();
41         if(done[u]) continue;
42         done[u] = true;
43         for(int i = head[u]; ~i; i = e[i].next) {
44             if(d[e[i].to] > d[u] + e[i].w) {
45                 d[e[i].to] = d[u] + e[i].w;
46                 q.push(PLI(d[e[i].to],e[i].to));
47             }
48         }
49     }
50 }
51 vector<int>ans;
52 int main() {
53     int kase,cs = 1;
54     scanf("%d",&kase);
55     while(kase--) {
56         scanf("%d%d",&n,&m);
57         memset(head,-1,sizeof head);
58         tot = 0;
59         for(int i = 1; i <= n; ++i) belong[i].clear();
60         for(int i = 1,t,s,tmp; i <= m; ++i) {
61             scanf("%d%d",&t,&s);
62             while(s--) {
63                 scanf("%d",&tmp);
64                 add(n + i,tmp,t);
65                 belong[tmp].push_back(i);
66             }
67         }
68         for(int i = 1; i <= n; ++i)
69             for(auto it:belong[i]) add(i,it + n,0);
70         memset(d,0x3f,sizeof d);
71         dijkstra(d[0],1);
72         dijkstra(d[1],n);
73         LL ret = INF;
74         ans.clear();
75         for(int i = 1; i <= n; ++i) {
76             if(max(d[0][i],d[1][i]) < ret) {
77                 ans.clear();
78                 ans.push_back(i);
79                 ret = max(d[0][i],d[1][i]);
80             } else if(max(d[0][i],d[1][i]) == ret) ans.push_back(i);
81         }
82         if(ret < INF) {
83             printf("Case #%d: %I64d\n",cs++,ret);
84             bool flag = false;
85             for(auto it:ans) {
86                 if(flag) putchar(‘ ‘);
87                 printf("%d",it);
88                 flag = true;
89             }
90             putchar(‘\n‘);
91         } else printf("Case #%d: Evil John\n",cs++);
92     }
93     return 0;
94 }

时间: 2024-10-22 00:14:25

HDU Meeting的相关文章

HDU 4311 Meeting point-1 &amp;&amp; HDU 4312 Meeting point-2

这俩个题  题意::给出N(<1e5)个点求找到一个点作为聚会的地方,使每个点到达这里的距离最小.4311是 曼哈顿距离 4312是 切比雪夫距离: 曼哈顿距离 :大家都知道 对于二维坐标系a(xa,yb),b(xb,yb)的曼哈顿距离是abs(xa-xb)+abs(ya-yb): 看的出来这个距离和x,y 都有关,但是X,Y并不相互影响,所以可以分开计算这样,分开计算的好处如下: 如果 只给一个维度的坐标系 ,我们是不可以再什么养的复杂度的时间内处理出来呢? 大难还是很好想的先排序一下,会发现

HDU 5521.Meeting 最短路模板题

Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3361    Accepted Submission(s): 1073 Problem Description Bessie and her friend Elsie decide to have a meeting. However, after Farmer Jo

codeforces 782B The Meeting Place Cannot Be Changed+hdu 4355+hdu 2438 (三分)

B. The Meeting Place Cannot Be Changed The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction. At some points on the road there are n

HDU 5521 Meeting(虚拟节点+最短路)

Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1358    Accepted Submission(s): 435 Problem Description Bessie and her friend Elsie decide to have a meeting. However, after Farmer Jo

HDU 5512 Meeting 博弈论

Meeting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5512 Description n pagodas were standing erect in Hong Jue Si between the Niushou Mountain and the Yuntai Mountain, labelled from 1 to n. However, only two

HDU 4311&amp;4312 Meeting point-1&amp;2 (曼哈顿距离&amp;&amp;切比雪夫距离)

HDU 4311 题意:平面上有n个点,一个点(x,y)只能到达(x-1,y), (x+1,y), (x, y-1), (x, y+1)4个点.从n个点中找到一点,使其他点到此点的距离之和最小. 思路: 可以发现,两个点间距离为 |x1-x2| + |y1-y2| ,这便是两点间的曼哈顿距离. 朴素的做法是遍历所有点,枚举该点与其他点间的曼哈顿距离之和,但是会TLE: 取巧的做法是将所有点与中心点的曼哈顿距离排序,枚举中间大概250个点左右的情况比较即可(不要欺负人家数据水! 正确姿势: 用结构

Hdu 5521 Meeting(建图+最短路)

题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=5521 思路:边数太多,不能直接建图.对于每个集合,设置一个虚拟点,对于每个集合中的点u:连一条u->S权值为0的边(点在集合中,花费为0):连一条S->u权值为w的边(从集合中一点到另一点花费w).分别计算从点1到i和从点n到i的最短路,枚举i,则ans=min( ans,max ( dist[0][i],dist[1][i] ) ). #include<queue> #i

【hdu 5521】【 2015ACM/ICPC亚洲区沈阳站重现赛】Meeting 题意&题解&代码

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5521 题意: 输入:输入n表示有n个点,输入m表示有m个点集. 接下来m行信息,每行先输入一个t表示这个点集中任意两点费时为t,再输入一个s,表示有s个点在这个点集中,接下来s个数表示这些数在这个点集之中. 现在有两个人,其中一个人住在点1,另一个人住在点n,如果两个人要见面,同时出发,可以走走停停,问需要最少时间是多少,有哪几个点能被当成见面点. 题解: 我们发现这道题如果建好图之后就直接是一个

hdu 5521 Meeting(最短路)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 题意:有1-n共n个点,给出m个块(完全图),并知道块内各点之间互相到达花费时间均为ti.已知两人分别在点1和点n,求在哪些点相遇能使得花费时间最短.题解:显然先想到从点1和点n分别求最短路,然后枚举点找出哪些点是相遇花费时间最少的.但是这题边太多了,假设一个完全图里有x个点,那边就有x*(x-1)/2条了,必须化简其边.一个可行的办法是给每个完全图增加两个点,分别为入点和出点,入点向其中的点