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 John decorated his
fences they were separated into different blocks. John's farm are divided into n blocks labelled from 1 to n.
Bessie lives in the first block while Elsie lives in the n-th one. They have a map of the farm
which shows that it takes they ti minutes to travel from a block in Ei to another block
in Ei where Ei (1≤i≤m) is a set of blocks. They want to know how soon they can meet each other
and which block should be chosen to have the meeting.
 
Input
The first line contains an integer T (1≤T≤6), the number of test cases. Then T test cases
follow.

The first line of input contains n and m. 2≤n≤105. The following m lines describe the sets Ei (1≤i≤m). Each line will contain two integers ti(1≤ti≤109)and Si (Si>0) firstly. Then Si integer follows which are the labels of blocks in Ei. It is guaranteed that ∑mi=1Si≤106.

 
Output
For each test case, if they cannot have the meeting, then output "Evil John" (without quotes) in one line.

Otherwise, output two lines. The first line contains an integer, the time it takes for they to meet.
The second line contains the numbers of blocks where they meet. If there are multiple
optional blocks, output all of them in ascending order.

 
Sample Input
2
5 4
1 3 1 2 3
2 2 3 4
10 2 1 5
3 3 3 4 5
3 1
1 2 1 2
 
Sample Output
Case #1: 3
3 4
Case #2: Evil John

Hint

In the first case, it will take Bessie 1 minute travelling to the 3rd block, and it will take Elsie 3 minutes travelling to the 3rd block. It will take Bessie 3 minutes travelling to the 4th block, and it will take Elsie 3 minutes travelling to the 4th block. In the second case, it is impossible for them to meet.

 
Source
 
题意:有m个集合,每个集合里面的任意两点均有一条距离为ei的无向边,求1和n到其他点的最短距离中最大值的最小值。
思路:最短路模板题。每个集合作为作为一个点,对应的点到集合的距离为ei,最后答案/2。
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<bitset>
#include<queue>
#include<stack>
#include<map>
#include<vector>
using namespace std;
#define eps 0.0000001
typedef long long ll;
typedef pair<int,int> P;
const int maxn=2e5+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e18+;
struct edge
{
int from,to;
ll w;
};
vector<edge>G[maxn];
priority_queue<P,vector<P>,greater<P> >q;
ll dist[][maxn];
void addedge(int u,int v,ll w)
{
G[u].push_back((edge)
{
u,v,w
});
G[v].push_back((edge)
{
v,u,w
});
}
void dij(int t,int s)
{
dist[t][s]=0LL;
q.push(P(dist[t][s],s));
while(!q.empty())
{
P p=q.top();
q.pop();
int u=p.second;
for(int i=; i<G[u].size(); i++)
{
edge e=G[u][i];
if(dist[t][e.to]>dist[t][u]+e.w)
{
dist[t][e.to]=dist[t][u]+e.w;
q.push(P(dist[t][e.to],e.to));
}
}
}
}
void init(int n)
{
for(int i=; i<=*n+; i++) G[i].clear();
}
int main()
{
int T;
scanf("%d",&T);
for(int Case=; Case<=T; Case++)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=; i<=m; i++)
{
int val;
scanf("%lld",&val);
int t;
scanf("%d",&t);
while(t--)
{
int s;
scanf("%d",&s);
addedge(s,n+i,val);
}
}
for(int i=; i<=*n+; i++) dist[][i]=dist[][i]=INF;
dij(,);
dij(,n);
ll ans=INF;
for(int i=; i<=n; i++)
{
//printf("%lld %lld\n",dist[0][i],dist[1][i]);
ans=min(ans,max(dist[][i],dist[][i]));
}
printf("Case #%d: ",Case);
if(ans>=INF) puts("Evil John");
else
{
printf("%lld\n",ans/);
int cou=;
for(int i=; i<=n; i++)
{
if(!cou&&max(dist[][i],dist[][i])==ans) printf("%d",i),cou++;
else if(cou&&max(dist[][i],dist[][i])==ans) printf(" %d",i),cou++;
}
printf("\n");
}
init(n);
}
return ;
}

最短路模板题

HDU 5521.Meeting 最短路模板题的更多相关文章

  1. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  2. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  3. 牛客小白月赛6 I 公交线路 最短路 模板题

    链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...

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

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  5. HDU 2544 最短路(模板题)

    求1到N的最短路径,模板题,以1为源点,用dijkstra算法(可以用优先级队列优化) #include <iostream> #include <algorithm> #in ...

  6. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  7. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  8. [USACO07FEB]银牛派对Silver Cow Party---最短路模板题

    银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...

  9. POJ 2387 Til the Cows Come Home --最短路模板题

    Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...

随机推荐

  1. ssh 制作免密登录(免密登录多台服务器)

    场景:服务器B (192.168.1.101)免密登录A(192.168.1.100)服务器 1.登录服务器B ①生产公钥 ssh-keygen -t rsa Enter file in which ...

  2. linux 磁盘 扩展分区

  3. 03-封装Response响应

    package com.day5; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputSt ...

  4. C#将对象序列化成JSON字符串

    C#将对象序列化成JSON字符串 public string GetJsonString() { List<Product> products = new List<Product& ...

  5. EL(Expression Language)和JSTL标签(JSP Standard Tag Library)

    一.EL表达式: Expression Language提供了在 JSP 脚本编制元素范围外(例如:脚本标签)使用运行时表达式的功能.脚本编制元素是指页面中能够用于在JSP 文件中嵌入 Java代码的 ...

  6. centos7 安装gitlab任意版本

    主要还是根据官网:https://www.gitlab.cc/installation/#centos-7 1.安装依赖: sudo yum install curl policycoreutils ...

  7. JAVA获取运行程序的src路径

    JavaPrj使用: System.getProperty("user.dir")+"\\src"或直接"\\src"JavaWeb使用Th ...

  8. OpenStack Trove组件WSGI和RPC调用流程(参考调用的整个过程)

    参考博文:https://blog.csdn.net/bill_xiang_/article/details/72909927

  9. 63.1拓展之纯 CSS 创作一个摇摇晃晃的 loader

    效果地址:https://scrimba.com/c/cqKv4VCR HTML code: <div class="loader"> <span>Load ...

  10. 6.HTML+CSS制作一双眼睛

    效果地址:https://codepen.io/flyingliao/pen/oOLodJ?editors=1100 其它动画效果地址:1.https://scrimba.com/c/cJ8NPpU2 ...