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 ...
随机推荐
- C++递归方法实现全排列
#include<iostream> using namespace std; void perm(int list[],int k,int m);//声明 void perm(int l ...
- Vue系列(三):组件及数据传递、路由、单文件组件、vue-cli脚手架
上一篇:Vue系列(二):发送Ajax.JSONP请求.Vue生命周期及实例属性和方法.自定义指令与过渡 一. 组件component 1. 什么是组件? 组件(Component)是 Vue.js ...
- Vue引用第三方datepicker插件无法监听datepicker输入框的值
一.背景 在Vue项目中使用了第三方的datepicker插件,在选择日期后vue无法检测到datepicker输入框的变化 <label class="fl">日期: ...
- mysql创建数据库并指定uft8编码
CREATE DATABASE IF NOT EXISTS ymk default character set utf8 COLLATE utf8_general_ci;
- 多个 WindowsFormsHost 叠加顺序调整
原文:多个 WindowsFormsHost 叠加顺序调整 工作中遇到多个 WindowsFormsHost 包装的控件叠加顺序的调整问题,用了 BingToFront 和 BringToBack,不 ...
- Java并发和多线程3:线程调度和有条件取消调度
在第1篇中"并发框架基本示例",提到了Executors和ThreadPool.其中,还有个"定时调度"的方法,Executors.newScheduledTh ...
- 聚类算法学习-kmeans,kmedoids,GMM
GMM参考这篇文章:Link 简单地说,k-means 的结果是每个数据点被 assign 到其中某一个 cluster 了,而 GMM 则给出这些数据点被 assign 到每个 cluster 的概 ...
- [环境搭建] VS-Visual Studio-IIS Express 支持局域网訪问
原创作品,转载请注明出处:http://blog.csdn.net/qiujuer/article/details/40350385 使用Visual Studio开发Web网页的时候有这种情况:想要 ...
- UVA 11294 - Wedding(Two-Set)
UVA 11294 - Wedding 题目链接 题意:有n对夫妻,0号是公主.如今有一些通奸关系(男男,女女也是可能的)然后要求人分配在两側.夫妻不能坐同一側.而且公主对面一側不能有两个同奸的人,问 ...
- 为powerpc交叉编译nginx
HOST: MINT NGINX VERSION: nginx-1.8.0(nginx-1.8.0.tar.gz) ZLIB VERSION: zlib-1.2.8 PCRE VERSION: pcr ...