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 ...
随机推荐
- GridView中某一列值的总和(web)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.R ...
- cocos2d-x学习之类型转换(转)
在做数据转换时,最好包含以下头文件 #include <iostream> #include <cmath> #include <string> #include ...
- hdoj 1686 kmp
题目: Sample Input 3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN Sample Output 1 3 0 代码: #in ...
- Facade 模式
在软件系统开发中经常回会遇到这样的情况,你实现了一些接口(模块),而这些接口(模块)都分布在几个类中(比如 A和 B.C.D) :A中实现了一些接口,B 中实现一些接口(或者 A代表一个独立模块,B. ...
- C# 窗体靠近屏幕边缘自动隐藏*学习(类似于QQ)
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; us ...
- php 上传视频的代码
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- Form表单三种提交按钮的区别?
1.<input type='button' id='btn' onclick='check()' value="提交"> 说明:只是普通的按钮(不附带提交功能),不会 ...
- JQuery解析HTML、JSON和XML实例详解
1.HTML 有的时候会将一段HTML片段保存在HTML文件中,在另外的主页面直接读取该HTML文件,然后解析里面的HTML代码片段融入到主页面中. fragment.html文件,其内容: 复制代码 ...
- 再次探究Android ListView缓存机制
概述 虽然现在5.0后Google推出了RecycleView,但在5.0 Lollipop普及前Listview仍会被广泛使用,所以打算再次探究一下Listview的源码,了解一下Listview ...
- java高精度进制转换
POJ1131 由于本题只有小数部分(整数部分均为0),故在进制转换的之后只能自己手写转换方法了. 8进制转换10进制的方法为,以0.75为例,应是7*8^-1 + 5*8^-2.所以呢,可以 ...