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 ...
随机推荐
- iOS 里面如何使用第三方应用程序打开自己的文件,调用wps其他应用打开当前应用里面的的ppt doc xls
我们的自己的应用里面经常涉及的要打开ppt doc,这样的功能,以前总以为iOS沙盒封闭化,不可能实现,后来终于解决了 使用 UIDocumentInteractionController 来解决这一 ...
- How to handle the DbEntityValidationException in C#
When I want to use db.SaveChanges(), if some of the columns got validation error and throw DbEntityV ...
- style currentStyle getComputedStyle的区别和用法
先介绍下层叠样式表的三种形式: 1.内联样式,在html标签中style属性设置. <p style="color:#f90">内联样式</p> 2.嵌入样 ...
- 《用chsh选择shell》-linux命令五分钟系列之十二
chsh命令用于修改你的登录shell. 1 我想知道我机器安装了哪些shell? 两种方法可以查看: 第一种: [rocrocket@wupengchong ~]$ chsh -l /bin/sh ...
- ios开发之UIImageView
废话少说,直接进入正题!!! 1.创建一个UIImageView: 创建一个UIImageView对象的几种方法: UIImageView *imageView1 = [[UIImageView al ...
- date日期比较和格式化方法
时间的比较 var now = new Date();//现在时间 var setDate = new Date(2017,0,5,17,55,55);//设定的时间 laert(now < s ...
- prototype/constructor/__proto__之prototype简单应用
一.简单使用构造原型加prototype造简单的轮子. 1.想jQ那样获取HTML元素,先看JS代码 function Cmf() { //创建构造函数 this.arry = [] } Cmf.pr ...
- Bootstrap 和 LESS
Bootstrap 简介 什么是 Bootstrap? Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的 ...
- SharePoint 设置Lookup 字段的值
如何设置Lookup字段的值, 首先我们同样需要了解SPFieldLookupValueCollection和SPFieldLookupValue, 这2个类的原理和之前所讲解到SPFieldUser ...
- NSNumber
integerfloatc 在Objective-c中有int的数据类型,那为什么还要使用数字对象NSNumber?这是因为很多类(如NSArray)都要求使用对象,而int不是对象.NSNumber ...