HDU4725 The Shortest Path in Nya Graph SPFA最短路
典型的最短路问题,但是多了一个条件,就是每个点属于一个layer,相邻的layer移动,如x层移到x+1层需要花费c.
一种显而易见的转化是我把这些边都建出来,但是最后可能会使得边变成O(n^2);
网上看到的一些做法就是拆点,假如我给每层做一个平台点,所有点都可以到这个平台,然后再换乘到别的平台里不就可以了吗? 所以对于属于第i层的点我们构造一个i层点的虚拟点,把这些点连到这个平台点,花费为0,相邻平台点之间的花费为c不就可以了吗? 但是问题就出现在这里了,这样的话同一层的点的花费就会变成0,原本可能不可达的变得可达。网上看到的拆点方法有这么两种。
A.每层拆两个点,一个点管入,一个点管出,这样的话同层的点不会回到同层的另外一个点上。
B.每层拆一个点,这个点只管入,而处于该层的点则向左右两层点相连。
A的话新建了2n个点,4n条边(两层相邻2n,每个点对应两条边2n,2n+2n=4n),B的话新建了n个点,5n条边(平台间2n条,每个点2n条,平台到点n条)。
不知道上面有没算错请指正。具体参考了下面的博客:
http://www.baidu.com/link?url=MV7krOHUY-l_IgU1_R5qKyUVgL5ZRprWE1IznI82Vwoo_RG_LrIaqxvCJismujP2TVaEEFg607BdhwWeUEy5aa
http://www.cnblogs.com/kuangbin/archive/2013/09/11/3315071.html
这道题当作是SPFA的模板练习题吧,晚了,睡觉去~
#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#define ll long long
#define maxn 300150
#define maxm 700100
#define inf 0x3f3f3f3f
using namespace std; int first[maxn];
int nxt[maxm];
int e;
int vv[maxm];
int cost[maxm];
int n, m, c;
int layer[maxn];
int dis[maxn];
bool in[maxn]; void addedge(int u, int v, int w)
{
vv[e] = v; cost[e] = w;
nxt[e] = first[u];
first[u] = e++;
} void spfa(int s)
{
queue<int> q;
memset(in, 0, sizeof(in));
memset(dis, 0x3f, sizeof(dis));
q.push(s); dis[s] = 0; in[s] = true;
while (!q.empty()){
int u = q.front(); q.pop();
in[u] = false;
for (int i = first[u]; i != -1; i=nxt[i]){
int v = vv[i];
if (dis[v] > dis[u] + cost[i]){
dis[v] = dis[u] + cost[i];
if (!in[v]) q.push(v), in[v] = true;
}
}
}
} int vlayer[maxn]; int main()
{
int T; cin >> T; int ca = 0;
while (T--)
{
e = 0; memset(first, -1, sizeof(first));
memset(vlayer, 0, sizeof(vlayer));
scanf("%d%d%d", &n, &m, &c);
for (int i = 1; i <= n; i++){
scanf("%d", layer + i);
vlayer[layer[i]] = 1;
}
for (int i = 1; i <= n-1; i++){
if (vlayer[i] && vlayer[i + 1]){
addedge(n + i, n + i + 1, c);
addedge(n + i + 1, n + i, c);
}
}
for (int i = 1; i <= n; i++){
addedge(n + layer[i], i, 0);
if (layer[i] > 1) addedge(i, n+layer[i] - 1, c);
if (layer[i] < n) addedge(i, n+layer[i] + 1, c);
}
int ui, vi, wi;
for (int i = 0; i < m; i++){
scanf("%d%d%d", &ui, &vi, &wi);
addedge(ui, vi, wi);
addedge(vi, ui, wi);
}
spfa(1);
int ans = dis[n];
if (ans<inf) printf("Case #%d: %d\n",++ca, dis[n]);
else printf("Case #%d: %d\n", ++ca, -1);
}
return 0;
}
HDU4725 The Shortest Path in Nya Graph SPFA最短路的更多相关文章
- hdu4725 The Shortest Path in Nya Graph【最短路+建图】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297574.html ---by 墨染之樱花 题目链接:http://acm.hdu ...
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- 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 (拆点+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 ...
- 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 ...
- ACM学习历程—HDU4725 The Shortest Path in Nya Graph(SPFA && 优先队列)
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- 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 建表与查询 HTML计算时间差
create database xue1 go --创建数据库 use xue1 go --引用数据库 create table xinxi ( code int, name ), xuehao ), ...
- linux中的namespace
本文将就namespace这个知识点,进行简单的归纳总结,力求通俗易通.在资料汇总的过程中,参考了许多网上的博客资料,在文章尾部给出相关链接. namespace,命名空间,从名字 ...
- java集合学习一
首先看一下java集合的关系图 1.1从全面了解Java的集合关系图.常见集合 list set map等其中我们最常用的 list map 结合.几天说一下常见的map.map在我工作的两年里 ...
- 解决Eclipse中Java工程间循环引用而报错的问题
如果myeclipse 报如下错误 A cycle was detected in the build path of project 如果我们的项目包含多个工程(project),而它们之间又是循 ...
- Linux C 程序 信号及信号的处理(19)
信号及信号的处理 1.Linux信号的介绍 信号是一种软件中断.Linux系统中根据POSIX标准扩展的信号机制. 1.信号来源 1.硬件方式 1.当用户按下某个键, ...
- Android混淆问题
最近做了2个项目,全部要混淆,刚接触,自己在网上找了还多资料,感觉各有千秋,自己总结了一下,第一次发帖,不喜勿喷.求各种指导!!! android应用程序的混淆打包规范 1.在工程文件project. ...
- php中each()与list()函数
<?php $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');reset($fruit); ...
- php 获取数组第一个值的方法分享
以下是对使用php实现获取数组第一个值的方法进行了详细的分析介绍,需要的朋友可以过来参考下 reset (PHP 3, PHP 4, PHP 5)reset -- 将数组的内部指针指向第一个单元 说明 ...
- Delphi XE5教程12:注释和编译器指示字
内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误!也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可 ...
- 小课堂Week11 会说话的代码
小课堂Week11 会说话的代码 今天主要讨论下,在编码过程中和"命名"相关的问题.因为命名方法比较自由,如果要提高可读性,我们需要尽量使其符合正规的英文语法习惯. 变量/属性 通 ...