Problem Description
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on.
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
The first line has a number T (T <= 20) , indicating the number of test cases.
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
For test case X, output "Case #X: " first, then output the minimum cost moving from node 1 to node N.
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
 
n个点,m条边,以及相邻层之间移动的代价c,给出每个点所在的层数,以及m条边,
每条边有u,v,c,表示从节点u到v(无向),并且移动的代价 c ,
问说从 1 到 n 的代价最小是多少。
将层抽象出来成为n个点(对应编号依次为n+1 ~ n+n),
然后层与层建边,点与点建边
,层与在该层上的点建边 (边长为0),点与相邻层建边 (边长为c)。
 
 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x7fffffff;
const int maxn = 2e5 + ;
int cas = , t, n, m, c, lv[maxn], have[maxn];
int tot, head[maxn], d[maxn], vis[maxn];
struct Edge {
int v, w, nxt;
} edge[maxn*];
struct node {
int v, d;
node(int v, int d) : v(v), d(d) {}
bool operator < (const node & a) const {
return d > a.d;
}
};
void init() {
tot = ;
mem(head, -);
}
void add(int u, int v, int w) {
edge[tot].v = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++;
}
int dijkstra(int st, int ed) {
mem(vis, );
for (int i = ; i < maxn ; i++) d[i] = INF;
priority_queue<node>q;
d[st] = ;
q.push(node(st, d[st]));
while(!q.empty()) {
node temp = q.top();
q.pop();
int u = temp.v;
if (vis[u]) continue;
vis[u] = ;
for (int i = head[u] ; ~i ; i = edge[i].nxt) {
int v = edge[i].v, w = edge[i].w;
if (d[v] > d[u] + w && !vis[v]) {
d[v] = d[u] + w;
q.push(node(v, d[v]));
}
}
}
return d[ed];
}
int main() {
sf(t);
while(t--) {
sfff(n, m, c);
init();
mem(have, );
mem(lv,);
for (int i = ; i <= n ; i++) {
sf(lv[i]);
have[lv[i]] = ;
}
for (int i = ; i <= m ; i++) {
int u, v, w;
sfff(u, v, w);
add(u, v, w);
add(v, u, w);
}
for (int i = ; i < n ; i++)
if (have[i] && have[i + ]) {
add(n + i, n + i + , c);
add(n + i + , n + i, c);
}
for (int i = ; i <= n ; i++) {
add(lv[i] + n, i, );
if (lv[i] > ) add(i, lv[i] + n - , c);
if (lv[i] < n) add(i, lv[i] + n + , c);
}
int ans = dijkstra(, n);
if (ans == INF) printf("Case #%d: -1\n", cas++);
else printf("Case #%d: %d\n", cas++, ans);
}
return ;
}
 
 
 

The Shortest Path in Nya Graph HDU - 4725的更多相关文章

  1. AC日记——The Shortest Path in Nya Graph hdu 4725

    4725 思路: 拆点建图跑最短路: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...

  2. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 (J ...

  9. HDU4725:The Shortest Path in Nya Graph(最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

随机推荐

  1. 【CQOI 2007】 余数求和

    题目描述 给出正整数n和k,计算G(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值,其中k mod i表示k除以i的余数.例如G(10, 5)=5 ...

  2. UVa 340 - Master-Mind Hints 解题报告 - C语言

    1.题目大意 比较给定序列和用户猜想的序列,统计有多少数字位置正确(x),有多少数字在两个序列中都出现过(y)但位置不对. 2.思路 这题自己思考的思路跟书上给的思路差不多.第一个小问题——位置正确的 ...

  3. Python3 数据类型-列表

    序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. 索引如下图: 列表命名(list): 组成:使用[]括起来,并且 ...

  4. onethink框架显示Access denied for user 'root'@'localhost' (using password: NO)

    本地开发的时候使用的用户名是root,密码为空,它会生成两份.一份在Common/config.php里面,还有一份在Application\User\Conf/config.php 在linux环境 ...

  5. bootstrap使用中遇到的坑

    一.例如: <div class="form-group"> <label class="control-label col-lg-3"> ...

  6. 开启假期JAVA之路

    . 从最基础的JAVA开始学起,已经上了三节课啦!希望在课程结束后能完成一个令自己满意的连连看项目,期待ing~ 慢慢的从简单的代码上手了~ . 用循环输出等腰三角形的效果 import java.u ...

  7. Spring Boot(二)配置分析

    回顾一下采用SSM开发项目时,项目中会存在多个配置文件,比如web.xml,配置Spring相关的applicationContext-springmvc.xml, applicationContex ...

  8. C#Color颜色表

    Color.AliceBlue 240,248,255 Color.LightSalmon 255,160,122 Color.AntiqueWhite 250,235,215 Color.Light ...

  9. Code Quality

    Code Quality https://www.sonarqube.org/ java https://www.sonarsource.com/products/codeanalyzers/sona ...

  10. WebExtensions & tabs.executeScript()

    tabs.executeScript() https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs ...