HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description
Input
Output
题目大意:有n个点m条无向边,然后每个点有一个层次,相邻层次的点可以移动(同层次不可以),花费为C,问1~n的最小花费。
思路:我试过类似于每层之间的点都连一条边(不是$O(n^2)$那种很挫的方法,不断TLE……好吧换思路……每层新建两个点a、b,i层的点到ai连一条费用为0的边,bi到i层的点连一条边,然后相邻的层分别从ai到bj连一条边,费用为C。跑Dijkstra+heap可AC。
PS:至于最初的思路为啥会TLE我就不想说了……有兴趣自己动脑吧……
代码(343MS):
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII; const int MAXN = ;
const int MAXE = MAXN * ; int head[MAXN];
int to[MAXE], next[MAXE], cost[MAXE];
int n, m, ecnt, lcnt, c; void init() {
memset(head, , sizeof(head));
ecnt = lcnt = ;
} inline void add_edge(int u, int v, int c) {
to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
} int dis[MAXN];
int lay[MAXN];
bool vis[MAXN]; void Dijkstra(int st, int ed) {
memset(dis, 0x7f, sizeof(dis));
memset(vis, , sizeof(vis));
priority_queue<PII> que; que.push(make_pair(, st));
dis[st] = ;
while(!que.empty()) {
int u = que.top().second; que.pop();
if(vis[u]) continue;
if(u == ed) return ;
vis[u] = true;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(dis[v] > dis[u] + cost[p]) {
dis[v] = dis[u] + cost[p];
que.push(make_pair(-dis[v], v));
}
}
}
} int T; inline int readint() {
char c = getchar();
while(!isdigit(c)) c = getchar();
int ret = ;
while(isdigit(c)) ret = ret * + c - '', c = getchar();
return ret;
} int main() {
T = readint();
for(int t = ; t <= T; ++t) {
n = readint(), m = readint(), c = readint();
init();
for(int i = ; i <= n; ++i) {
lay[i] = readint();
add_edge(i, n + * lay[i] - , );
add_edge(n + * lay[i], i, );
}
for(int i = ; i < n; ++i) {
add_edge(n + * i - , n + * (i + ), c);
add_edge(n + * (i + ) - , n + * i, c);
}
int u, v, w;
while(m--) {
//scanf("%d%d%d", &u, &v, &w);
u = readint(), v = readint(), w = readint();
add_edge(u, v, w);
add_edge(v, u, w);
}
Dijkstra(, n);
if(dis[n] == 0x7f7f7f7f) dis[n] = -;
printf("Case #%d: %d\n", t, dis[n]);
}
}
HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)的更多相关文章
- HDU 4717 The Moving Points(三分法)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description There are N points in total. Every point moves in certain direction and certain speed. W ...
- HDU 4722 Good Numbers(位数DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description If we sum up every digit of a number and the result can be exactly divided by 10, we say ...
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]
HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...
- HDU 4725 The Shortest Path in Nya Graph
he Shortest Path in Nya Graph Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged o ...
- 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 ...
- 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 ...
- 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 ...
- (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
随机推荐
- IOS NSNotification 通知
一. 先看下官方对NSNotification通知的解释 1. NSNotification 通知 @interface NSNotification : NSObject <NSCopying ...
- c# 开发可替换的通用序列化库
开篇继续吹牛.... 其实没有什么可吹的了,哈哈哈哈哈 主要是写一个通用库,既可以直接用,又方便替换,我的序列化都是采用第三方的哈. 我不上完整代码了,只是给大家讲讲过程. 1.写一个序列化的类,我是 ...
- Linux入门-第三周
1.总结vim命令行模式常见快捷方式,以及vim查找,替换的方法 vim [options] [file ..] +# 打开文件后,让光标处于第#行的行首,(默认行尾) 举例vim +10 /etc/ ...
- web pack
WebPack是模块捆绑器,如果你的代码跨越了不同模块(例如不同Javascript文件),web pack可以将这些零散的代码构建到浏览器可读单个文件中. web pack还可以作为构建通道,你可以 ...
- QQ运动,新楛的马桶还在香,营销人不应摒弃。
QQ运动,都说新楛的马桶还香三天,为毛你这般明日黄花,为营销人所弃. QQ运动,一个差不多被遗忘的冷却地带,却圈粉无数,以性感.狂野.妖艳.线条.汗水等秀元素贯穿始终,狼友显露于此,爱美的女性也未曾缺 ...
- Volatile的详解
volatile关键字修饰的共享变量主要有两个特点:1.保证了不同线程访问的内存可见性 2.禁止重排序 在说内存可见性和有序性之前,我们有必要看一下Java的内存模型(注意和JVM内存模型的区分 ...
- python应用:经纬度匹配
需要安装第三方包:requests 本次经纬度匹配采用高德地图api,首先将gps坐标转化为高德地图的经纬度坐标,然后再根据转化后的坐标进行匹配. 本次匹配主要是获取距离给定经纬度最近的poi点地址信 ...
- python网络编程的坑(持续更新)
初学python,踩了许多坑...每天都学一点吧..(大佬绕过) 1.session的用法: session是python requests库中的一个重要功能.session可以存储用户的数据并且存储 ...
- hdu 1394 Minimum Inversion Number(线段树)
参考:http://blog.sina.com.cn/s/blog_691ce2b70101ldmm.html https://blog.csdn.net/wiking__acm/article/de ...
- TensorFlow深层神经网络常用方法
深度学习所示深层神经网络的代名词,重要特性:多层.非线性. 若只通过线性变换,任意层的神经网络模型与单层神经网络模型的表达能力没有任何区别,这是线性模型的局限性.对于线性可分的问题中,线性模型可解决, ...