学习最短路建图 HUD 5521
http://acm.hdu.edu.cn/showproblem.php?pid=5521
题目大意:有n个点,m个集合,每个集合里面的点都两两可达且每条边权值都是val,有两个人A, B,A在pos=1,B在pos=n,问两者相遇的最短时间,输出相遇地点,如果有多个最短时间,输出的相遇地点按从小到大排序。
思路:因为集合暴力枚举点肯定mle。所以我不会TAT(我好菜啊)。于是看了一下别人的想法, 就是新建一个节点,然后让集合里面的所有节点都连向他, 然后权值定为和原来的权值一样,也是val(但是最后的ans要除以2)。 TAT,既然我都已经刷过网络流了,既然这个都没有想到
接下来就简单了。。。dijstra跑两次就好了= =
//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const LL inf = 1e17;
const int maxn = 2e6 + ;
struct Edge{
int from, to; LL val;
Edge(int f = , int t = , LL val = ): from(f), to(t), val(val){}
};
struct Point{
int from; LL val;
Point(int f = , LL val = ): from(f), val(val){}
bool operator < (const Point &a) const{
return val > a.val;
}
};
vector<Edge> G[maxn];
int kase, n, m;
int a[maxn]; void build(int newpoint, int cnt, LL t){
for (int i = ; i <= cnt; i++){
G[a[i]].push_back(Edge(a[i], newpoint, t));
G[newpoint].push_back(Edge(newpoint, a[i], t));
}
}
LL d[maxn], d1[maxn], d2[maxn];
void dijstra(int s){
for (int i = ; i <= n + m; i++){
d[i] = inf;
}
d[s] = ;
priority_queue<Point> que;
que.push(Point(s, d[s]));
while (!que.empty()){
Point u = que.top(); que.pop();
int len = G[u.from].size();
for (int i = ; i < len; i++){
Edge p = G[u.from][i];
int v = p.to;
if (d[v] > d[u.from] + p.val){
d[v] = d[u.from] + p.val;
que.push(Point(v, d[v]));
}
}
}
} void solve(){
dijstra();
for (int i = ; i <= n; i++) d1[i] = d[i];
dijstra(n);
for (int i = ; i <= n; i++) d2[i] = d[i];
LL minival = inf;
for (int i = ; i <= n; i++){
minival = min(minival, max(d1[i], d2[i]));
}
if (minival == inf) {
printf("Case #%d: Evil John\n", ++kase);
return ;
}
vector<int> v;
for (int i = ; i <= n; i++){
if (minival == max(d1[i], d2[i])) v.push_back(i);
}
printf("Case #%d: %I64d\n", ++kase, minival / );
for (int i = ; i < v.size(); i++){
printf("%d%c", v[i], i == v.size() - ? '\n' : ' ');
}
} int main(){
int T; cin >> T;
while (T--){
scanf("%d%d", &n, &m);
for (int i = ; i <= n + m; i++) G[i].clear();
for (int i = ; i <= m; i++){
LL t; int cnt; scanf("%I64d%d", &t, &cnt);
for (int j = ; j <= cnt; j++){
scanf("%d", a + j);
}
build(i + n, cnt, t);
}
solve();
}
return ;
}
学习最短路建图 HUD 5521的更多相关文章
- HDU 5521 [图论][最短路][建图灵感]
/* 思前想后 还是决定坚持写博客吧... 题意: n个点,m个集合.每个集合里边的点是联通的且任意两点之间有一条dis[i]的边(每个集合一个dis[i]) 求同时从第1个点和第n个点出发的两个人相 ...
- hdu4725 The Shortest Path in Nya Graph【最短路+建图】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297574.html ---by 墨染之樱花 题目链接:http://acm.hdu ...
- Codeforces 938D. Buy a Ticket (最短路+建图)
<题目链接> 题目大意: 有n座城市,每一个城市都有一个听演唱会的价格,这n座城市由m条无向边连接,每天变都有其对应的边权.现在要求出每个城市的人,看一场演唱会的最小价值(总共花费的价值= ...
- HDU5521-最短路-建图
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- hdu 5294 Tricks Device 最短路建图+最小割
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Tricks Device Time Limit: 2000/1000 MS (Java/Other ...
- hdu 4725 The Shortest Path in Nya Graph (最短路+建图)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 第八届河南省赛C.最少换乘(最短路建图)
C.最少换乘 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 94 Solved: 25 [Submit][Status][Web Board] De ...
- 『The Captain 最短路建图优化』
The Captain(BZOJ 4152) Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小 ...
- HDU-4725.TheShortestPathinNyaGraph(最短路 + 建图)
本题思路:主要是建图比较麻烦,因为结点可以在层与层之间走动,也可以在边上进行走动,所以主要就是需要找到一个将结点和层统一化处理的方法. 所以我们就可以对于存在边的结点建边,层与层之间如果层数相差一也建 ...
随机推荐
- 5个可用提高Godaddy主机速度的第三方CDN加速服务商
毕竟Godaddy主机数据中心位于美国.新加坡.欧洲三个数据中心,一般我们都会选择美国数据中心,相比较其他2个机房的速度是快和稳定的,很多人要说为什么新加坡数据中心速度不好呢?因为目前的新加坡机房不是 ...
- 第三节,入门知识和windows系统安装python环境
1.使用Linux的好处(稳定)不容易死机,可以长达几年不间断运行(安全)相对windows系统更安全,相对更不容受到各种攻击(开源)免费使用2.安装好虚拟机VMware软件,和Linux系统,以及X ...
- [妙味JS基础]第一课:属性操作、图片切换、短信发送模拟
知识点总结 HTML的属性操作:读.写 元素.属性名 => “读” 元素.属性名=新的值 => “写” 例如: oBtn.value => “读” oBtn.value='按钮' = ...
- 修改win7锁定界面背景
Regedit HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Authentication/LogonUI/Backgrou ...
- 转:MongoDB介绍及下载与安装
非原创,我也是转载(Here)过来备份一下.关于MongoDB园子里有个系列讲的不错的,点击此处跳转 MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系 ...
- 浙江大学Pat 1036 题解
1036. Boys vs Girls (25) This time you are asked to tell the difference between the lowest grade of ...
- Babel 相关资料
Babel online editor Babel Plugin Handbook babeljs usage options
- file_zilla 通过key连接远程服务器
file_zilla 通过key连接 01 在putty中 ifconfig -a 查看当前网站ip02 文件-站点管理器--新建站点---主机ip 端口2203协议 SFTP 就是SSH协议04登录 ...
- uhttpd配置文件分析
文件位于 /etc/config/uhttpd. root@hbg:/etc/config# cat uhttpd config uhttpd 'main' list listen_ht ...
- HDU 1509 Windows Message Queue(队列)
题目链接 Problem Description Message queue is the basic fundamental of windows system. For each process, ...