ACM学习历程—HDU4725 The Shortest Path in Nya Graph(SPFA && 优先队列)
Description
Input
Output
Sample Input
Sample Output
题目大意是是有n层。然后每层会有部分点,点与点可以通过特点的代价互达,相邻层之间的点可以通过C代价到达。
题目一开始没有看到每层可以有多个点。还有需要注意的是同层之间的点不能直接0代价到达。
之前在没考虑同层间不能直接0代价互达的时候,是把每个层看作一个节点,这样某层的节点到这个节点的代价都是0。
如果考虑到这个条件的话,
所以需要构造出下面这样的路径:

为每层设置From和To节点。某层的节点只能从From节点前往,或者前往To节点。
而相邻层之间亦是如此。
这样就能避免同层代价为0这个问题了。
为了避免点的冲突,From从n开始往后,To从2n开始往后。
需要注意的是这样操作以后点的数目上界会变成3N,线段就是6N。
这里采用链式前向星存图,采用STL优先队列的SPFA。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <vector>
#define N 300005 using namespace std; typedef pair<int, int> pii; struct Edge
{
int to;
int next;
int val;
}edge[*N]; int head[N], cnt; void addEdge(int u, int v, int w)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
edge[cnt].val = w;
head[u] = cnt;
cnt++;
} int dis[N];
bool vis[N];
int n, m, c; inline int idFrom(int i)
{
return i + n;
} inline int idTo(int i)
{
return i + *n;
} void Input()
{
memset(head, -, sizeof(head));
memset(dis, -, sizeof(dis));
memset(vis, , sizeof(vis));
dis[] = ;
cnt = ;
int k, v, w;
scanf("%d%d%d", &n, &m, &c);
for (int i = ; i <= n; ++i)
{
scanf("%d", &k);
addEdge(idFrom(k), i, );
addEdge(i, idTo(k), );
}
for (int i = ; i < n; ++i)
{
addEdge(idTo(i), idFrom(i+), c);
addEdge(idTo(i+), idFrom(i), c);
}
for (int i = ; i < m; ++i)
{
scanf("%d%d%d", &k, &v, &w);
addEdge(k, v, w);
addEdge(v, k, w);
}
} void Work()
{
pii k;
priority_queue <pii, vector<pii>, greater<pii> > q;
q.push(pii(, ));
while (!q.empty())
{
k = q.top();
q.pop();
int x = k.second;
if (vis[x])
continue;
vis[x] = true;
for (int i = head[x]; i != -; i = edge[i].next)
{
if (dis[edge[i].to] != - &&
dis[edge[i].to] <= dis[x] + edge[i].val)
continue;
if (vis[edge[i].to])
continue;
dis[edge[i].to] = dis[x] + edge[i].val;
q.push(pii(dis[edge[i].to], edge[i].to));
}
}
printf("%d\n", dis[n]);
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
printf("Case #%d: ", times);
Input();
Work();
}
return ;
}
ACM学习历程—HDU4725 The Shortest Path in Nya Graph(SPFA && 优先队列)的更多相关文章
- HDU4725 The Shortest Path in Nya Graph SPFA最短路
典型的最短路问题,但是多了一个条件,就是每个点属于一个layer,相邻的layer移动,如x层移到x+1层需要花费c. 一种显而易见的转化是我把这些边都建出来,但是最后可能会使得边变成O(n^2); ...
- 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 (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- HDU-4725 The Shortest Path in Nya Graph 最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 如果直接建图复杂度过大,但是考虑到每层之间的有效边很少,只要在每层增加两个虚拟节点n+i和2*n ...
- hdu4725 The Shortest Path in Nya Graph【最短路+建图】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297574.html ---by 墨染之樱花 题目链接:http://acm.hdu ...
- HDU4725 The Shortest Path in Nya Graph dij
分析:对于每一层,原来n个点,然后扩展为原来的三倍,每一层扩展一个入点,一个出点,然后跑最短路 注:tmd我把一个n写成m了,然后wa了7次,我都要怀疑人生了 #include<cstdio&g ...
- hdu4725 The Shortest Path in Nya Graph
这道题看了下很多人都是把每一层拆成两个点然后建图做的. 我的思路很直接,也不用建图,直接在更新每个点时更新他相邻的边和相邻的层,当然前提是每个点只更新一次,每个层也只更新一次,这样才能确保时间复杂度. ...
- 2013成都邀请赛J称号||HDU4725 The Shortest Path in Nya Graph(spfa+slf最短的优化)
职务地址:HDU 4725 这题卡了好长时间了,建图倒是会建,可是不会最短路的算法优化,本以为都须要堆去优化的,打算学了堆之后再来优化.可是昨晚CF的一道题..(那题也是不优化过不了..)然后我就知道 ...
- 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 ...
随机推荐
- DDR电源硬件设计要点
一.DDR电源简介 1. 电源 DDR的电源可以分为三类: a.主电源VDD和VDDQ,主电源的要求是VDDQ=VDD,VDDQ是给IO buffer供电的电源,VDD是给但是一般的使用中都是把VDD ...
- 转:PCIe基础知识
PCIe基础知识 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/zqixiao_09/article/details/51842542 PCIe ...
- 安装CoreOS到磁盘
1 打开翻&&墙软件 2 打开迅雷,启用“使用IE代理”,下载以下两个文件:(翻&&墙后用IE下会中断) http://stable.release.core-os.n ...
- spring-web中的WebDataBinder理解
Spring可以自动封装Bean,也就是说前台通过SpringMVC传递过来的属性值会自动对应到对象中的属性并封装成javaBean,但是只能是基本数据类型(int,String等).如果传递过来的是 ...
- POJ 2456 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- 【React Native开发】React Native控件之RefreshControl组件具体解释(21)
),React Native技术交流4群(458982758)种 enabled bool android平台适用 用来设置下拉刷新功能是否可用 progressBackgroundColo ...
- WPF自定义搜索框代码分享
首先下载搜索图标: 控件中的搜索图标下载地址:http://www.easyicon.net/1183666-Search_icon.html 搜索框设计过程比较简单: 1.先定义一个Rectangl ...
- CString 成员函数用法大全(转)
CString( );例:CString csStr; CString( const CString& stringSrc );例:CString csStr("ABCDEF中文12 ...
- EasyNVR H5无插件摄像机直播解决方案前端解析之:引用videojs无法自动播放
关于videojs自动播放问题 播放流媒体多使用videojs来进行播放,videojs,本身自带自动播放属性: 通过添加autoplay(),来完成视频播放的自动加载: player = video ...
- 九度OJ 1059:abc (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3642 解决:2869 题目描述: 设a.b.c均是0到9之间的数字,abc.bcc是两个三位数,且有:abc+bcc=532.求满足条件的 ...