典型的最短路问题,但是多了一个条件,就是每个点属于一个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. 水仙花数 java 实现

    题目描述: 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3.现在要求 ...

  2. sql常用的星期方法

    sql常用的星期方法: SELECT convert(varchar(10),DATEADD(wk, DATEDIFF(wk,0,getdate()), 0),120) --本周开始周一SELECT ...

  3. no permissions fastboot

    no permissions  fastboot 获取fastboot文件 1.编译后得android源码会在目录: andsource2/out/host/linux-x86/bin 产生fastb ...

  4. Oracle中排序列中值相同引发的问题(译)

    This queston came up on the Oracle newsgroup a few days ago: 这个问题在Oracle的新闻中心被提出了一段时间: I have a tabl ...

  5. wordpress修改固定链接及修改链接后链接提示404错误的解决办法

    wordpress默认的url实在是不好看又不好记忆,而且还不利于SEO.因此,我就捣鼓着把url做一个自定义.自定义的方式如下: 建议使用/%postname%的形式,这样利于SEO. 修改之后,l ...

  6. 分享:mysql 随机查询数据

    在mysql中查询5条不重复的数据,使用以下: 1 SELECT * FROM `table` ORDER BY RAND() LIMIT 5  就可以了.但是真正测试一下才发现这样效率非常低.一个1 ...

  7. svn 清空

    SVN是目前用得比较多的而且很方便的版本管理体系. 在开发过程中遇到了这样的问题: 有时我们需要一个干净的code版本,没有 .svn 这些文件夹记录的版本传到服务器上使用. 这个时候自己一个个去删除 ...

  8. TextView中gravity属性值测定

    Attributes Explain top 不改变控件大小,对齐到容器顶部 bottom 不改变控件大小,对齐到容器底部 left 不改变控件大小,对齐到容器左侧 right 不改变控件大小,对齐到 ...

  9. c# 刻度:毫米 英寸 像素转换

    从目前所掌握的资料来看,c#程序中将毫米转换像素的方法无非两种: 第一种: 1: /// <summary> 2: /// 以毫米为单位的显示宽度 3: /// </summary& ...

  10. WPF中ListBox的项ListBoxItem被选中的时候Background变化

    使用WPF 中ListBox,点击ListBoxItem的时候,自定义它的背景色,曾经在网上找了一些方法, 不是很理想,后来在StackOverflow上找到了,贴出代码和效果图: 效果图: