The Shortest Path in Nya Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2296    Accepted Submission(s): 561

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
 
Source
 
Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  4822 4821 4820 4819 4818 
 
最短路
把每一层拆成2 个点 一个是连接结点入边,一个是连接结点出边,每层连接结点入边的点连接下个层连接结点出边的点,下一层连接入边的点连接上一层连接出边的点。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue> using namespace std; const int MAX_N = 3e5 + ;
const int INF = 1e9 + ;
typedef long long ll;
struct heapnode {
int d;
int u;
bool operator < (const heapnode &rhs) const {
return d > rhs.d;
}
};
struct Edge {int from, to, cost;}; vector<int> G[MAX_N];
vector<Edge> edges;
int N,M,C;
int d[MAX_N];
bool done[MAX_N];
vector <int> lay[MAX_N]; void add_edge(int from, int to, int cost) {
edges.push_back((Edge){from, to, cost});
int m = edges.size();
G[from].push_back(m - );
} void dij(int s) {
memset(done,,sizeof(done));
for(int i = ; i <= * N; ++i) {
d[i] = INF;
}
d[s] = ;
priority_queue <heapnode> q;
q.push((heapnode) {d[s], s}); while(!q.empty()) {
heapnode x = q.top(); q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = ;
if(u == N) return; for(int i = ; i < G[u].size(); ++i) {
Edge &e = edges[ G[u][i] ];
if(d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
q.push((heapnode) {d[e.to], e.to});
}
}
}
}
int main()
{ // freopen("sw.in","r",stdin);
int t;
scanf("%d",&t);
int ca = ;
while(t--) {
scanf("%d%d%d",&N,&M,&C);
for(int i = ; i <= * N; ++i) G[i].clear();
edges.clear(); for(int i = ; i <= N; ++i) {
int ch;
scanf("%d",&ch);
add_edge(i, ch + N, );
add_edge(ch + * N, i, ); } /*for(int i = 2 * N + 1; i <= 3 * N; ++i) {
add_edge(i, i - N, 0);
}*/ for(int i = N + ; i <= * N - ; ++i) {
add_edge(i, i + + N, C);
add_edge(i + , i + N, C);
} for(int i = ; i <= M; ++i) {
int u, v, w;
scanf("%d%d%d",&u, &v, &w);
add_edge(u, v, w);
add_edge(v, u, w);
} dij();
printf("Case #%d: %d\n",ca++, (d[N] == INF) ? - : d[N]);
} return ;
}

hdu 4725的更多相关文章

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

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

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

  3. HDU 4725 The Shortest Path in Nya Graph(最短路拆点)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:n个点,某个点属于某一层.共有n层.第i层的点到第i+1层的点和到第i-1层的点的代价均是 ...

  4. HDU 4725 The Shortest Path in Nya Graph-【SPFA最短路】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:有N个点和N层..一层有X个点(0<=X<=N).两邻两层间有一条路花费C.还有M ...

  5. HDU 4725 The Shortest Path in Nya Graph(spfa+虚拟点建图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题目大意:有n层,n个点分布在这些层上,相邻层的点是可以联通的且距离为c,还有额外给出了m个条边 ...

  6. HDU 4725 建图

    http://acm.hdu.edu.cn/showproblem.php?pid=4725 The Shortest Path in Nya Graph Time Limit: 2000/1000 ...

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

  8. hdu 4725 The Shortest Path in Nya Graph(建图+优先队列dijstra)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:有n个点和n层,m条边,每一层的任意一个点都可以花费固定的值到下一层或者上一层的任意点 然 ...

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

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

  10. kuangbin_ShortPath P (HDU 4725)

    很有挑战的一题 直接暴力建图的话毫无疑问O(n^2)会TLE 每层虚拟一个点又会让没有点的层也能连过去 参考kuangbin菊苣的方法每层用了两个虚拟点 n+i*2-1 是入口 n+i*2 是出口 然 ...

随机推荐

  1. MvvmCross for WPF 支持子窗体显示、关闭、传参

    最近在做 PCL(Portable Class Library)平台的项目,所以发一下自己遇到的问题 MvvmCross 是 PCL 平台的一个 MVVM 框架 地址:https://github.c ...

  2. 删除字符串第一个byte

    删除字符串第一个byte   一种方式:   char * mag; char buff[1000]; char number; memcpy((char *)msg,buff,len); strnc ...

  3. HDU1006

    Problem Description The three hands of the clock are rotating every second and meeting each other ma ...

  4. Weka链接Mysql数据库

    Weka简介 Weka的全名是怀卡托智能分析环境(Waikato Environment for Knowledge Analysis),是一款免费的,非商业化(与之对应的是SPSS公司商业数据挖掘产 ...

  5. 基于AppCan MAS系统,如何轻松实现移动应用数据服务?

    完成一个移动应用开发,前端提供页面展示,当它要与一些业务系统进行交互,又该如何实现呢?2016AppCan移动开发者大会上,AppCan前端开发经理杨庆,分享了AppCan轻松实现移动应用数据服务的方 ...

  6. iOS学习之C语言函数

    一.函数的定义 返回值类型 函数名(参数类型 参数名, ...) { 功能语句; return 返回值; } 按照返回值和参数划分: 第一种: 无返回值 无参 void sayHello() { pr ...

  7. Ubuntu系统安装配置Pintos和Bochs

    Ubuntu系统安装配置 Pintos 和 Bochs 安装过程 首先是UEFI启动模式下Win8.1安装Ubuntu14.04双系统,由于篇幅过长,就不在这里详写.可见博主的另一篇博客http:// ...

  8. 【转载】如何在FPGA设计环境中添加加时序约束

    转自:http://bbs.ednchina.com/BLOG_ARTICLE_198929.HTM 如何在FPGA设计环境中加时序约束    在给FPGA做逻辑综合和布局布线时,需要在工具中设定时序 ...

  9. Mysql去除重复

    常用的有两种方法,第一种就是select distinct name from table.但是有时候我们要返回多个字段时就用第二种方法select *, count(distinct name) f ...

  10. 在Eclipse中制作SSH配置文件提示插件

    原文地址:http://blog.csdn.net/longyuhome/article/details/8968093 这篇博客算是对原先的“在Eclipse中制作和使用struts2配置文件提示插 ...