典型的最短路问题,但是多了一个条件,就是每个点属于一个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最短路的更多相关文章

  1. hdu4725 The Shortest Path in Nya Graph【最短路+建图】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297574.html      ---by 墨染之樱花 题目链接:http://acm.hdu ...

  2. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

  3. 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 ...

  4. 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 题意: 在一个图中跑最 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. poj 2010 Moo University - Financial Aid

                                                                                                Moo Univ ...

  2. 1_1准备工作[wp8特色开发与编程技巧]

    1准备工作 大家好,我是徐文康,今天我要开始带大家玩转windowsphone8 app的开发 在这一套视频中,我将带大家从零开始学习编程.在互联网时代熟悉编程是非常有必要的.差异化竞争将变成趋势,那 ...

  3. aspx利用cookie值来停止silverlight中的计时器

    一.silverlight与silverlight中可以利用委托(delegate)来刷新frame.Refresh() 1.在子类中定义委托捕捉关闭事件按钮 public delegate void ...

  4. C#简单实现发送手机短信

    偶然想起,像编写一个从电脑向手机发送短信的程序,从网上查找到有三种方式:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2) ...

  5. vc列表控件的初始化

    void CManageProcessDlg::InitList() {  m_ListProcess.SetExtendedStyle(m_ListProcess.GetExtendedStyle( ...

  6. hibernate4 使用及 新特性

    hibernate4.x已经在官网出现一段时间了.下载地址: http://hibernate.org/orm/downloads/ 使用hibernate4所需要的jar包 在lib\require ...

  7. Python中Cookie的处理(一)Cookie库

    Cookie用于服务器实现会话,用户登录及相关功能时进行状态管理.要在用户浏览器上安装cookie,HTTP服务器向HTTP响应添加类似以下内容的HTTP报头: Set-Cookie:session= ...

  8. 1049. Counting Ones/整数中1出现的次数(从1到n整数中1出现的次数)

    The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...

  9. C#二维数组及其本质(转)

    C#中二维数组包含两类:二维数组和数据矩阵.(这是我个人分类法,我认为比较能反映本质). 如上图,是二维数组,横向为第一维度,纵向为第二维度,不同维度可以有不同长度. 如果去掉元素7,那么上图也可能是 ...

  10. 查看表空间信息SQL集合

    1.查看表空间的名称及大小 SELECT t.tablespace_name as "表空间名", )), ) AS "所占物理空间M" FROM dba_ta ...