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
#include <cstdio>
#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,c; void init()
{
memset(head, , sizeof(head));
ecnt = ;
} 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));
}
}
}
return ;
}
/*有n个点m条无向边,每个点有一个层次,相邻层次的点可以移动,花费为C,问1~n的最小花费。
思路:每层新建两个点a、b,i层的点到ai连一条费用为0的边,bi到i层的点连一条费用为0的边,
然后相邻的层分别从ai到bj连一条边,费用为C。跑Dijkstra+heap可AC。*/
int main()
{
int T;
scanf("%d",&T);
for(int t = ; t <= T; ++t)
{
scanf("%d %d %d",&n,&m,&c);
init();
for(int i = ; i <= n; ++i)
{
scanf("%d",&lay[i]);
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);
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]);
}
return ;
}

最短路(HDU4725)

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

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

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

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

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

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

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

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

  7. HDU-4725 The Shortest Path in Nya Graph (拆点+dji)

    HDU 4725 The Shortest Path in Nya Graph : http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意: 在一个图中跑最 ...

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

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

随机推荐

  1. asp.net 获取当前项目路径

    方法一://获取当前项目的路径System.AppDomain.CurrentDomain.BaseDirectory.ToString();   // 得到的是当前项目的根目录取的值:F://Pro ...

  2. iframe 适用高度

    contentWindow 兼容各个浏览器,可取得子窗口的 window 对象. contentDocument Firefox 支持,> ie8 的ie支持.可取得子窗口的 document ...

  3. (转载)delphi 中如何调用sql 存储过程

    delphi 中如何调用sql 存储过程 使用TADOStoredProc组件,可以,给你举个例子好了 with ADOStoredProc1 do begin Close; Parameters.C ...

  4. C语言数据结构之栈:括号匹配

    括号匹配这是个很简单的题目,如果只有小括号,就模拟进栈和出栈的过程就行了: 注:输入时'@'作为结束标志 #include <stdio.h> int main() { freopen(& ...

  5. 2014年度辛星css教程夏季版第六节

    这一节我们就要讲到布局了,其实布局本身特别简单,但是要合理的布好局就不那么简单了,就像我们写文章一样,写一篇文章非常简单,但是要写一篇名著就很难了,这需要我们扎实的功底和对文学的理解,但是,千里之行, ...

  6. ee_15_mvc_db_page----demo---bai

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  7. 关于appStore评分的相关说明--转自张诚教授

    在iOS7以前,评分地址如下 itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?typ ...

  8. 自适应单本小说网站源码,基于bootstrap+dedecms。

    具体效果:http://www.ishengxu.cc/ 基于bootstrap+dedecms,PC端与手机端自适应,广告位也都设计好了,很简单.

  9. java-development.sh

    vi /etc/profile.d/java-development.sh export JAVA_HOME=/usr/local/java/jdk1..0_55 export JRE_HOME=$J ...

  10. Unity C#写的A*寻路

    原地址:http://www.unity蛮牛.com/blog-13769-1078.html 首先看了这篇翻译外国人的文章http://www.raywenderlich.com/zh-hans/2 ...