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 ...
随机推荐
- 用cudamat做矩阵运算的GPU加速
1. cudamat简介 cudamat是一个python语言下,利用NVIDIA的cuda sdk 进行矩阵运算加速的库.对于不熟悉cuda编程的程序员来说,这是一个非常方便的GPU加速方案.很多工 ...
- spring4.0源码导入
一个面试,让我知道了自己的不足,一天不进步就是倒退. spring源码导入eclipse 本人的环境 (我导入的是最新的spring 4.0 所以要用jdk1.8) 1 安装git (mac上自带了g ...
- 【转】IL编织 借助PostSharp程序集实现AOP
ref: C# AOP实现方法拦截器 在写程序的时候,很多方法都加了.日志信息.比如打印方法开始,方法结束,错误信息,等等. 由于辅助性功能的代码几乎是完全相同的,这样就会令同样的代码在各个函数中 ...
- php 文件上传简单类---限制仅上传jpg文件
php 文件上传代码,限制只能上传jpg格式文件,也可以自行添加其它扩展名的文件. <?php /* * 图片上传类 仅限JPG格式图片 * edit by www.jbxue.com at 2 ...
- Python的类实例方法,类方法,类静态方法
以下面的类定义为例: # coding:utf-8 class A: count = 0 def __init__(self, inst_name): self.inst_name = inst_na ...
- 把数组排成最小的数/1038. Recover the Smallest Number
题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. Give ...
- 删除或清空具有外键约束的表数据报-ERROR 1701 (42000)
OS: centos 6.3 DB:5.5.14 mysql> select database();+------------+| database() |+------------+| sa ...
- WebDev.WebServer40.exe已停止工作
今天写程序的遇到这个错误 错误的原因是代码中有死循环
- db2查看表空间
select substr(tbsp_name,1,20) as 表空间名称,substr(tbsp_content_type,1,10) as 表空间类型,sum(tbsp_total_size_k ...
- iOS 8安装教程图解
苹果最新移动设备操作系统iOS 8终于在今天开放下载.相较于iOS 7,iOS 8此次最大的变化包括苹果全新的健康应用.允许用户跨设备操作的连续性(Continuity)功能等. iOS 8的整体软件 ...