hdu 4725
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
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.
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
Case #2: 3
#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的更多相关文章
- 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(最短路拆点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:n个点,某个点属于某一层.共有n层.第i层的点到第i+1层的点和到第i-1层的点的代价均是 ...
- 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 ...
- HDU 4725 The Shortest Path in Nya Graph(spfa+虚拟点建图)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题目大意:有n层,n个点分布在这些层上,相邻层的点是可以联通的且距离为c,还有额外给出了m个条边 ...
- HDU 4725 建图
http://acm.hdu.edu.cn/showproblem.php?pid=4725 The Shortest Path in Nya Graph Time Limit: 2000/1000 ...
- 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(建图+优先队列dijstra)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:有n个点和n层,m条边,每一层的任意一个点都可以花费固定的值到下一层或者上一层的任意点 然 ...
- AC日记——The Shortest Path in Nya Graph hdu 4725
4725 思路: 拆点建图跑最短路: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...
- kuangbin_ShortPath P (HDU 4725)
很有挑战的一题 直接暴力建图的话毫无疑问O(n^2)会TLE 每层虚拟一个点又会让没有点的层也能连过去 参考kuangbin菊苣的方法每层用了两个虚拟点 n+i*2-1 是入口 n+i*2 是出口 然 ...
随机推荐
- <?php>慢慢写一些php的cookie问题<?>
写网站是个爬坑的过程,在你设计完功能之后,就会发现:卧槽,这个怎么实现?你妹,这个能实现么? 进了公司分工明确还好说(= =学长们都这么说),在学校自己没事写一些项目的话只能自己爬坑了. 蹬蹬瞪蹬,登 ...
- rspec的一些常见用法
这里讲了如何安装rspec,安装使用rspec. 下面介绍一下rspec中常见的使用方法. 下面是一个最简单的测试用例,判断true是不是等于true,should_be是旧的用法,新用法推荐使用ex ...
- Linux中printf格式化输出
printf使用文本或者由空格分隔的参数,我们可以在printf中使用格式化字符串.printf不会写像echo那样自动添加换行符,必须手动添加 =========================== ...
- MongoDB分片简单实例
分片 在Mongodb里面存在另一种集群,就是分片技术,可以满足MongoDB数据量大量增长的需求. 当MongoDB存储海量的数据时,一台机器可能不足以存储数据也足以提供可接受的读写吞吐量.这时,我 ...
- dede 忘记密码在数据库中修改方法
如何找回或修改dedecms后台管理员登录密码呢? 一个客户把密码忘了,找了很长一会没几个靠谱的回答,dede是使用md5加密,但是,它是显示32位md5加密码从第6位开始的20位 方法是直接修改其m ...
- supplicant
概述 wpa_supplicant是wifi客户端(client)加密认证工具,和iwconfig不同,wpa_supplicant支持wep.wpa.wpa2等完整的加密认证,而iwconfig只能 ...
- Knockout应用开发指南 第一章:入门
2011-11-21 14:20 by 汤姆大叔, 20165 阅读, 17 评论, 收藏, 编辑 1 Knockout简介 (Introduction) Knockout是一个轻量级的UI类 ...
- Socket与TcpClient的区别(转载)
Socket和TcpClient有什么区别 原文:http://wxwinter.spaces.live.com/blog/cns!C36588978AFC344A!322.entry 回答: &qu ...
- iOS学习之C语言循环结构
一.while循环 while (循环条件) { 循环体: } // 1.定义循环变量 int time = 1; // 2.循环条件 while ( ...
- why does angular js rock
angularjs 入门教程 http://angular-tips.com/blog/2013/08/why-does-angular-dot-js-rock/ Practive the previ ...