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 ...
随机推荐
- Spark源码分析之五:Task调度(一)
在前四篇博文中,我们分析了Job提交运行总流程的第一阶段Stage划分与提交,它又被细化为三个分阶段: 1.Job的调度模型与运行反馈: 2.Stage划分: 3.Stage提交:对应TaskSet的 ...
- PHPExcel简易使用教程
在企业里使用PHP进行开发,不可避免总会遇到读/写Excel的需求,遇到这种需求,一般使用PHPExcel类库进行开发. PHPExcel现在最新版本是1.8.0,最低需要PHP5.2版本,支持读取x ...
- golang生成随机函数的实现
golang生成随机数可以使用math/rand包, 示例如下: package main import ( "fmt" "math/rand" ) func ...
- poj 2096 Collecting Bugs 【概率DP】【逆向递推求期望】
Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 3523 Accepted: 1740 ...
- java jdbc oracle ORA-01795: 列表中的最大表达式数为 1000
在操作SQL中存在In的数量如果超过1000条会提示 ORA-01795: 列表中的最大表达式数为 1000 归纳有几种方式出现的: 第一种是:我在上一个 [jdbc 同时执行 查询和删除操]作中 ...
- HibernateTemplate方法的使用
1.查询帖子(Post)为例 查找所有的帖子 public List<Post> findPosts() { String hql = "from Post p left joi ...
- XShell 连接 vm虚拟机中的redhat Linux系统
选择的是nat链接,因为nat链接是没有网络的情况下,也是可以链接操作的,当然bridge也可以,那我就从第一步开始; 因为有的人可能改过电脑上的虚拟适配器的ip地址,导致和虚拟机默认的不一样了.如果 ...
- TCP/UDP server
Simple: Sample TCP/UDP server https://msdn.microsoft.com/en-us/library/aa231754(v=vs.60).aspx Simple ...
- 【题解】CF264B Good Sequences
[题解]CF264B Good Sequences 具有很明显的无后效性. 考虑\(dp\). 考虑初始条件,显然是\(dp(0)=0\) 考虑转移,显然是\(dp(t)=max(dp[k])+1\) ...
- 【题解】P2279消防局的设立
[题解][P2279 HNOI2003]消防局的设立 又是一道贪心. 随便指定一个点为根,可以知道在覆盖了一个节点的子树的情况下,消防站越高越好.那么我们就贪心吧.\(trick\)是按深度\(pus ...