The Shortest Path in Nya Graph
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.
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.
If there are no solutions, output -1.
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
#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的更多相关文章
- 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 ...
- 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 ...
- 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
he Shortest Path in Nya Graph Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged o ...
- 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 题意: 在一个图中跑最 ...
- 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 (J ...
随机推荐
- WINIO64位模拟键鼠操作
最近玩游戏有点入迷,心急升级经验,于是某天开始"挂尸体",一系列手动操作之后决定写一个外挂,模拟一系列键盘和鼠标操作,没做过游戏winform也用得少,开始以为只要简单调用winA ...
- C语言之广度优先算法
广度优先算法又称宽度优先搜索,是一种简便的图的搜索算法之一.搜索方式大致是这样的: 直到搜索到目标结点(结点就是那些圆球球,其中有一个或者多个是目标结点)或者搜完了整个图都没找到目标结点就停止搜索. ...
- 4.MVC框架开发(母版页的应用、按钮导致的Action处理、从界面向控制器传数据和HtmlHelper控件的实现(注册的实现))
1.在视图里如何引入母版页 1)在视图里母版页都是放在View目录下面的Shared文件夹下面 2)母版页里的RenderBody()类似于ASP.NET里面的ContentPalceHolder占位 ...
- 一步步学习NHibernate(2)——配置NHibernate的环境
请注明转载地址:http://www.cnblogs.com/arhat 第二章 环境搭建 在上一章中,我们知道了NHibernate是用来干什么的了,那么今天在本章中,我们开始搭建NHibernat ...
- LogBack入数据库重写
项目需要:将info以及error的日志信息写入到数据库中:同时所有的日志都要写入到日志文件中. 可以封装一下,在基类的logError/logInfo中调用了log.error()以及log.inf ...
- CSS随手记
html5模板 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...
- 使用ssh公钥实现免密码登录
使用ssh公钥实现免密码登录 ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 有机器A(10.207.160.34),B(10 ...
- tomcat+apache 实现负载均衡之一:同一台电脑部署2个以上tomcat
1. 下载tomcat 8.0.17 http://apache.fayea.com/tomcat/tomcat-8/v8.0.17/bin/apache-tomcat-8.0.17.tar.gz ...
- Tomacat服务器的安装和配置
一, Tomcat服务器的下载地址(Apache Tomcat的官网): http://tomcat.apache.org/download-70.cgi 这里为了稳定性安装的版本为7.0. 截止目 ...
- xxx couldn't be loaded because it has not been added to the build settings.
这个由于没有将进入场景放入Build Settings里面造成的.