P - The Shortest Path in Nya Graph-hdu4725(双端队列+拆点)
题意:有N个点和N层..一层有X个点(0<=X<=N).两邻两层间有一条路花费C。还有M条小路在两个点之间。问从第一个点走到第N个点最短路是多少...
可以考虑在每一层增加一个点,这个点到上下层的距离是C,与本层的距离是0;
T的很惨,不太明白为什么,翻了一下大神的博客,发现这个要把每层拆成两个点来算的,要是只拆成一个点那么本层到本层的点都会是0了
#include<stdio.h>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std; const int maxn = 1000005;
const int maxm = 100005;
const int oo = 0xfffffff; struct node
{
int u, v, c, next;
}e[maxn];
int head[maxm*3], dis[maxm*3];
bool use[maxm*3]; void Add(int u, int v, int w, int k)
{
e[k].u = u;
e[k].v = v;
e[k].c = w;
e[k].next = head[u];
head[u] = k;
}
void spfa()
{
deque<int> Q;
Q.push_back(1); while(Q.size())
{
int i = Q.front();Q.pop_front();
use[i] = false; for(int j=head[i]; j!=0; j=e[j].next)
{
int u=e[j].u, v=e[j].v, w=e[j].c; if(dis[u]+w < dis[v])
{
dis[v] = dis[u] + w; if(use[v] == false)
{
use[v] = true;
if(Q.size() && dis[v] < dis[Q.front()])
Q.push_front(v);
else
Q.push_back(v);
}
}
}
}
} int main()
{
int T, t=1; scanf("%d", &T); while(T--)
{
int i, N, M, C, u, v, w, x, k=1; scanf("%d%d%d", &N, &M, &C); memset(head, 0, sizeof(head)); for(i=1; i<=N; i++)
{
//本层拆出来的点是 出i+N, 入i+2*N dis[i] = dis[i+N] = dis[i+2*N] = oo; if(i != N)
{
Add(i+N, i+2*N+1, C, k++);
Add(i+N+1, i+2*N, C, k++);
} scanf("%d", &x);//节点i属于第x层 Add(i, x+N, 0, k++);
Add(x+2*N, i, 0, k++);
} for(i=1; i<=M; i++)
{
scanf("%d%d%d", &u, &v, &w);
Add(u, v, w, k++);
Add(v, u, w, k++);
} dis[1] = 0;
spfa(); if(dis[N] == oo)
printf("Case #%d: -1\n", t++);
else
printf("Case #%d: %d\n", t++, dis[N]);
} return 0;
}
P - The Shortest Path in Nya Graph-hdu4725(双端队列+拆点)的更多相关文章
- 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 (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- 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 ...
- HDU 4725 The Shortest Path in Nya Graph
he Shortest Path in Nya Graph Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged o ...
- 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 (J ...
随机推荐
- java Web Services搭建环境时遇到的各种问题,记录一下。 java.lang.OutOfMemoryError: PermGen space,org/apache/struts2/util/ObjectFactoryDestroyable
情况:在同一个,myEclipes 下加载俩个项目,一个seriver端,一个client端. 必备: myEclipes ,apache-tomcat-7.0.42,apache-tomcat ...
- 最简单的基于FFmpeg的移动端例子:IOS 视频转码器
===================================================== 最简单的基于FFmpeg的移动端例子系列文章列表: 最简单的基于FFmpeg的移动端例子:A ...
- 子树大小平衡树(Size Balanced Tree,SBT)操作模板及杂谈
基础知识(包括但不限于:二叉查找树是啥,SBT又是啥反正又不能吃,平衡树怎么旋转,等等)在这里就不(lan)予(de)赘(duo)述(xie)了. 先贴代码(数组模拟): int seed; int ...
- 【BZOJ2752】【线段树】高速公路
Description Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这条高速公路上设立了许多收费站. Y901高速公路是一条由N-1段路以及N个 ...
- 一个IP支持多个网站实例(Apache2、Ubuntu相关)
http://www.blogjava.net/Andyluo/archive/2009/08/24/21821.html http://blog.csdn.net/zltianhen/article ...
- 页面加载完成,但ie进度条一直加载
页面ajax执行完删除等操作,会刷新当前页面,如果前端框架是左右iframe格式 我的前端页面提示用asyncBox,可能iframe和asyncBox的影响,出现这种情况: 网上大多数的说法是 页面 ...
- C#接收post数据
private string PostInput() { try { System.IO.Stream s=Request.InputStream; ; ]; StringBuilder builde ...
- MongoDB 复制集模式Replica Sets
1.概述 复制集是一个带有故障转移的主从集群.是从现有的主从模式演变而来,增加了自动故障转移和节点成员自动恢复. 复制集模式中没有固定的主结点,在启动后,多个服务节点间将自动选举 产生一个主结点.该主 ...
- PHP学习之中数组-遍历一维数组【2】
在PHP学习之中数组[1]中学会怎么创建一个数组,如果PHP学习之中数组[1]中的元素多的话,我们访问元素又是一个问题了,下面我们就使用for语句while,foreach来遍历我们的数组: < ...
- 【Python开发实战】Python环境的配置
1. 安装Pythonsudo aptitude -y install python-dev 安装Distribute:支撑模块构建与导入的包sudo chmod -R 0775 /usr/local ...