SPFA(建图) HDOJ 4725 The Shortest Path in Nya Graph
题意:有两种路径,每个点会分别在某一层,层相邻之间权值c.还有直接两点传送,花费w.问1到n的最短距离.
分析:1~n正常建边.然后n + a[i]表示i点在第a[i]层.然后再优化些就不会超时了.
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <queue>
- using namespace std;
- const int N = 2e5 + 5;
- const int E = N * 20;
- const int INF = 0x3f3f3f3f;
- struct Edge {
- int v, w, nex;
- Edge() {}
- Edge(int v, int w, int nex) : v (v), w (w), nex (nex) {}
- }edge[E];
- int head[N];
- int d[N];
- int a[N];
- bool vv[N];
- bool vis[N];
- int n, m, e;
- void init(void) {
- memset (head, -1, sizeof (head));
- memset (vv, false, sizeof (vv));
- e = 0;
- }
- void add_edge(int u, int v, int w) {
- edge[e] = Edge (v, w, head[u]);
- head[u] = e++;
- }
- void SPFA(int s) {
- memset (vis, false, sizeof (vis));
- memset (d, INF, sizeof (d));
- d[s] = 0; vis[s] = true;
- queue<int> que; que.push (s);
- while (!que.empty ()) {
- int u = que.front (); que.pop ();
- vis[u] = false;
- for (int i=head[u]; ~i; i=edge[i].nex) {
- int v = edge[i].v, w = edge[i].w;
- if (d[v] > d[u] + w) {
- d[v] = d[u] + w;
- if (!vis[v]) {
- vis[v] = true; que.push (v);
- }
- }
- }
- }
- }
- int main(void) {
- int T, cas = 0; scanf ("%d", &T);
- while (T--) {
- init ();
- int c; scanf ("%d%d%d", &n, &m, &c);
- for (int i=1; i<=n; ++i) {
- scanf ("%d", &a[i]);
- vv[a[i]] = true;
- }
- for (int i=1; i<n; ++i) {
- if (vv[i] && vv[i+1]) {
- add_edge (n+i, n+i+1, c);
- add_edge (n+i+1, n+i, c);
- }
- }
- for (int i=1; i<=n; ++i) {
- add_edge (n+a[i], i, 0);
- if (a[i] > 1) add_edge (i, n+a[i]-1, c);
- if (a[i] < n) add_edge (i, n+a[i]+1, c);
- }
- for (int u, v, w, i=1; i<=m; ++i) {
- scanf ("%d%d%d", &u, &v, &w);
- add_edge (u, v, w); add_edge (v, u, w);
- }
- SPFA (1);
- printf ("Case #%d: %d\n", ++cas, d[n] == INF ? -1 : d[n]);
- }
- return 0;
- }
SPFA(建图) HDOJ 4725 The Shortest Path in Nya Graph的更多相关文章
- 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 (最短路+建图)
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 ...
- 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
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(spfa+虚拟点建图)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题目大意:有n层,n个点分布在这些层上,相邻层的点是可以联通的且距离为c,还有额外给出了m个条边 ...
- HDU 4725 The Shortest Path in Nya Graph( 建图 + 最短路 )
主要是建图,建好图之后跑一边dijkstra即可. 一共3N个点,1~N是原图中的点1~N,然后把每层x拆成两个点(N+x)[用于连指向x层的边]和(N+N+x)[用于连从x层指出的边]. 相邻层节点 ...
- HDU 4725 The Shortest Path in Nya Graph (最短路 )
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...
随机推荐
- Spring面向切面编程(AOP)方式二
使用注解进行实现:减少xml文件的配置. 1 建立切面类 不需要实现任何特定接口,按照需要自己定义通知. package org.guangsoft.utils; import java.util.D ...
- 关闭Eclipse的控制台console自动跳出
一.背景 在eclipse中进行开发,尤其是在后台有项目运行的时候,当有log或者错误需要打印到console中时,控制台就会被自动弹出,恰好这时候你又在编写代码,就会感觉瞬间想杀人,下面我们就来分享 ...
- [Android] adb 命令 dumpsys activity , 用来看 task 中的activity。 (uninstall virus)
用“adb shell dumpsys activity”命令再来查看一下系统运行的的任务,就会看到: ACTIVITY MANAGER ACTIVITIES (dumpsys activity ac ...
- September 23rd 2016 Week 39th Friday
Even a small star shines in the darkness. 星星再小,也会发光. In the darkness, even a small star can shine. N ...
- qt_计算器的简单实现
//阶乘不知道怎么实现不了/(ㄒoㄒ)/~~,以后慢慢调试吧......... //转换为后缀表达式,实现最主要功能 void MainWindow::toPostfix () { QString e ...
- 第一课 移动端&响应式
一.调试工具介绍(Chrome Emulation) 1.Device(设备相关) 自定义尺寸.Network(网络模拟).UseAgent(浏览器信息).缩放 2.Media(媒体) 3.Netwo ...
- php 投票系统练习
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Redis笔记(七)Java实现Redis消息队列
这里我使用Redis的发布.订阅功能实现简单的消息队列,基本的命令有publish.subscribe等. 在Jedis中,有对应的java方法,但是只能发布字符串消息.为了传输对象,需要将对象进行序 ...
- .net学习之母版页执行顺序、jsonp跨域请求原理、IsPostBack原理、服务器端控件按钮Button点击时的过程、缓存、IHttpModule 过滤器
1.WebForm使用母版页后执行的顺序是先执行子页面中的Page_Load,再执行母版页中的Page_Load,请求是先生成母版页的控件树,然后将子页面生成的控件树填充到母版页中,最后输出 2.We ...
- Filp Game
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25573 Accepted: 11052 题目链接: ...