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. ## 在webapp上使用input:file, 指定capture属性调用默认相机,摄像,录音功能

    在iOS6下开发webapp,使用inputz之file,很有用 <input type="file" accept="image/*" capture= ...

  2. spark写入ES(动态模板)

    使用es-hadoop插件,主要使用elasticsearch-spark-20_2.11-6.2.x.jar 官网:https://www.elastic.co/guide/en/elasticse ...

  3. 最小生成树(Kruskal和Prim算法)

    关于图的几个概念定义:          关于图的几个概念定义: 连通图:在无向图中,若任意两个顶点vi与vj都有路径相通,则称该无向图为连通图. 强连通图:在有向图中,若任意两个顶点vi与vj都有路 ...

  4. 福大软工1816 · 第五次作业 - 结对作业2_map与unordered map的比较测试

    测试代码: #include <iostream> using namespace std; #include <string> #include <windows.h& ...

  5. Swift中避免在多个文件中重复import相同的第三方包

    swift中由于有命名空间的存在,在同一个target创建的文件,都可以不引用直接就可以拿来使用,但是不同target之间必须要import 之后才能使用,在不同的文件中使用都要重复的import这个 ...

  6. Qt代码覆盖率code coverage(VS版)

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt代码覆盖率code coverage(VS版)     本文地址:http://techi ...

  7. RPC架构-美团,京东面试题目

    RPC(Remote Procedure Call) RPC服务 从三个角度来介绍RPC服务:分别是RPC架构,同步异步调用以及流行的RPC框架. RPC架构 先说说RPC服务的基本架构吧.允许我可耻 ...

  8. 【问题解决】Project facet Java version 1.7 (或者1.8)is not supported.

    在移植eclipse项目时,如果遇到 “Project facet Java version 1.7 is not supported.” 项目中的jdk1.7不支持.说明项目是其他版本jdk编译的, ...

  9. DELPHI的MEMO组件

    位于Standard选项卡上,它是对EDIT控件的扩展,可以对多行文本进行显示.输入 和编辑. Lines属性: 该属性实际上为TStrings类型的对象,用来存放Memo对象的文本 TStrings ...

  10. CCS3 动画-鼠标放上去放大背景图片

    ---〉 效果如上,一个简单的过渡放大效果, <!DOCTYPE HTML> <html> <body> <style> #test{ width:30 ...