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 | CAShapeLayer转场动画
什么也不说了,作为一名乐于分享技术的小开发,直接先上个样式最为直观贴切,有需要的朋友可以直接拿过去用. 需要demo请点击这里 :github 在这个demo中,核心为选用画布CAShapeLayer ...
- Struts2知识点小结(二)
一.结果视图的配置 <result name="success">/success.jsp</result> 1.局部结果视图 ...
- Sass 基础(五)
@if @if 指令是一个SassScript,它可以根据条件处理样式块,如果条件为true返回一个样式块,反之 false 返回另一个样式块,在Sass 中除了@if之,还可以配合@else if和 ...
- ES6 async await
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JS中对象继承方式
JS对象继承方式 摘自<JavaScript的对象继承方式,有几种写法>,作者:peakedness 链接:https://my.oschina.net/u/3970421/blog/28 ...
- A+B Problem(高精)
题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑负数[/color][/b] 输入输出格式 输入格式: 分两行输入a,b<=10^500 ...
- Keepalived 配置高可用
VRRP协议及Keepalived原理使用 VRRP 协议即 Virtual Router Redundancy Protocol,虚拟路由器冗余协议, 为了解决局域网内默认网关单点失效的问题. ...
- ABAP术语-Object Type
Object Type 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/06/1093159.html Description created ...
- DOM查询
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- WebMagic 启动例子报错
报错内容: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/Http ...