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 ...
随机推荐
- SQL Server output经典使用
output经典使用 分类: sql2012-02-16 18:17 409人阅读 评论(0) 收藏 举报 outputinserttabledeletegonull OUTPUT是SQL SERVE ...
- Linux(CentOS)系统下设置nginx开机自启动
Nginx 是一个很强大的高性能Web和反向代理服务器.下面介绍在linux下安装后,如何设置开机自启动.首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令:vi ...
- hdu 2159 FATE
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2159 思路:二维完全背包,状态转移方程为: f[j][l]=max(f[j][l],f[j-b[i]] ...
- php 关联数据库的留言板练习
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 12.享元模式(Flyweight Pattern)
using System; using System.Collections; namespace ConsoleApplication5 { class Program { /// <summ ...
- 11.外观模式(Facade Pattern)
using System; namespace ConsoleApplication4 { class Program { /// <summary> /// 不使用外观模式的情况 /// ...
- Delphi中DLL的创建和使用
参考:http://blog.csdn.net/ninetowns2008/article/details/6311663 结合这篇博客:http://www.cnblogs.com/xumenger ...
- .NET NLog 详解(四) - filter
我们将版本向前切换到20051025,这期的关注点是filter.我们在使用日志的时候可能希望加上一些过滤器,在满足某些特定条件的时候才输出.举个简单的使用方式如下: <nlog> < ...
- wp8 入门到精通 Animation 背景加字体颜色从下向上变化颜色效果
<phone:PhoneApplicationPage.Resources> <Style x:Key="ButtonStyle1" TargetType=&qu ...
- PHP实现上一篇、下一篇
//php实现上一篇.下一篇 获取当前浏览文章id $id = isset($_GET[ ? intval($_GET['id']) : ""; 下一篇文章 $query = my ...