HDU 4725 The Shortest Path in Nya Graph
he Shortest Path in Nya Graph
This problem will be judged on HDU. Original ID: 4725
64-bit integer IO format: %I64d Java class name: Main
The Nya graph is an undirected graph with "layers". Each node in the graph belongs to a layer, there are N nodes in total.
You can move from any node in layer x to any node in layer x + 1, with cost C, since the roads are bi-directional, moving from layer x + 1 to layer x is also allowed with the same cost.
Besides, there are M extra edges, each connecting a pair of node u and v, with cost w.
Help us calculate the shortest path from node 1 to node N.
Input
For each test case, first line has three numbers N, M (0 <= N, M <= 105) and C(1 <= C <= 103), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers li (1 <= li <= N), which is the layer of ith node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 104), which means there is an extra edge, connecting a pair of node u and v, with cost w.
Output
If there are no solutions, output -1.
Sample Input
2
3 3 3
1 3 2
1 2 1
2 3 1
1 3 3 3 3 3
1 3 2
1 2 2
2 3 2
1 3 4
Sample Output
Case #1: 2
Case #2: 3
Source
#include <bits/stdc++.h>
#define pii pair<int,int>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
struct arc{
int to,w,next;
arc(int x = ,int y = ,int z = -){
to = x;
w = y;
next = z;
}
}e[];
int head[maxn],d[maxn],tot,n,m,c;
int layer[maxn];
void add(int u,int v,int w){
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
}
bool done[maxn];
priority_queue< pii,vector< pii >,greater< pii > >q;
int dijkstra(int s,int t){
while(!q.empty()) q.pop();
memset(d,0x3f,sizeof d);
memset(done,false,sizeof done);
q.push(pii(d[s] = ,s));
while(!q.empty()){
int u = q.top().second;
q.pop();
if(done[u]) continue;
done[u] = true;
for(int i = head[u]; ~i; i = e[i].next){
if(d[e[i].to] > d[u] + e[i].w){
d[e[i].to] = d[u] + e[i].w;
q.push(pii(d[e[i].to],e[i].to));
}
} }
return d[t] == INF?-:d[t];
}
bool hslv[maxn];
int main(){
int kase,tmp,u,v,w,cs = ;
scanf("%d",&kase);
while(kase--){
memset(head,-,sizeof head);
memset(hslv,false,sizeof hslv);
tot = ;
scanf("%d%d%d",&n,&m,&c);
for(int i = ; i <= n; ++i){
scanf("%d",&tmp);
layer[i] = tmp;
hslv[tmp] = true;
}
for(int i = ; i < m; ++i){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
for(int i = ; i <= n; ++i){
add(layer[i]+n,i,);
if(layer[i] > ) add(i,layer[i]-+n,c);
if(layer[i] < n) add(i,layer[i]+n+,c);
}
printf("Case #%d: %d\n",cs++,dijkstra(,n));
}
return ;
}
HDU 4725 The Shortest Path in Nya Graph的更多相关文章
- 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(构图)
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 ...
- HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- HDU 4725 The Shortest Path in Nya Graph (最短路 )
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...
- HDU - 4725 The Shortest Path in Nya Graph 【拆点 + dijkstra】
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...
随机推荐
- IOS - 退出程序
- (void)exitApplication { OAAppDelegate *app = [UIApplication sharedApplication].delegate; UIWindow ...
- Vue学习之路第一篇(学习准备)
1.开发工具的选择 这个和个人的开发习惯有关,并不做强求,厉害的话用记事本也可以.但是我还是建议用人气比较高的编辑工具,毕竟功能比较全面,开发起来效率比较高. 我之前写前端一直用的是sublimete ...
- Layui父页面向子页面传参
废话不多说!直接上代码! 父窗体js $('.mytable').on('click', '.editRow', function () { var table = $('#table_id_exam ...
- Python学习笔记(4)列表
2019-02-26 列表(list):①创建方法:用‘[ ]’,将数据包括起来,数据之间用逗号隔开.②空列表:empty = []③增删改查: 1)增加: a.append()方法——将元素添加到列 ...
- tp框架报错 Namespace declaration statement has to be the very first statement in the script
Namespace declaration statement has to be the very first statement in the script tp框架报这个错误,错误行数就是nam ...
- b.WHERE使用中多行子查询(适用于in,any,all条件)
b.多行子查询(适用于in,any,all条件) //查询与SCOTT和MARTIN在同一个部门的同事的编号和名称 select empno,ename from emp where ...
- Nginx +Tomcat 实现动静态分离(转)
Nginx +Tomcat 实现动静态分离 动静态分离就是Nginx处理客户端的请求的静态页面(html页面)或者图片,Tomcat处理客户端请求的动态页面(jsp页面),因为Nginx处理的静态页面 ...
- leetCode(24):Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- Linux进程的内存布局
这张图很好,注意其中最上面是高位地址,虽然很多个0,但是c开头的,不要看反了: 更具体的可以看这里: A.正文段.这是由cpu执行的机器指令部分.通常,正文段是可共享的,所以即使是经常执行的程序(如文 ...
- OS 中文斜体 Italic Font Chinese - iOS_Girl
CGAffineTransform matrix = CGAffineTransformMake(1, 0, tanf(15 * (CGFloat)M_PI / 180), 1, 0, 0); UI ...