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 ...
随机推荐
- Hash - a javascript dictionary object.
Hash,in wikipedia, may relevant to many stuffs. In javascript, hash is a group of name/value pairs w ...
- 使用XLinq.XElement读取带Namespace(命名空间)的XML
简介 本文主要介绍通过XELemet去读取含有namespaces(xmlns)的XML,这也是偶然间发现这个问题的,一个群里的小伙伴突然问起这个,以此记录一下. 背景 一个XML文档可能包括来自多个 ...
- iOS 代码分类
控件分类: 指示器 (ActivityIndicator) 提醒对话框 (AlertView) 按钮 (Button) 日历 (Calendar) 相机 (Camera) 透明指示层 (HUD) 图像 ...
- Objective-C 实例方法可见度,方法
一 实例方法可见度,方法 1.实例变量的可见度 可见度 特点 ...
- Mysql group_concat函数被截断的问题
mysql group_concat函数被截断的问题 MySQL的 group_concat 函数默认返回1024个字节长度,超过长度的会被截断.最近程序中就遇到这个问题了. 通过如下命令可以查看 ...
- 数组Api .map()的使用
之前并没有过多的使用过这个Api,在此记录下对其的理解,方便以后多多使用. 首先是对map的说明: var mappedArray = array.map(callback[, thisObject] ...
- @import————————css代码内部链接另外css
在css代码里这样可以链接另外的css @import url("style.css"); @import语法结构 @import + 空格+ url(CSS文件路径地址); ...
- innerHtml写法
swt_center = "<div id='new_swt_wee'>"; swt_center += '<a href="javascript:vo ...
- 【转】ThinkPHP中数据库操作返回值总结
Thinkphp中的Think\Model类提供了数据库的基本CURD(Create.Update.Read及Delete),通过该类可以很便捷的进行操作.Model类及扩展类主要的方法有: Crea ...
- jquery元素查找方法集锦
jQuery常用的元素查找方法总结 $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到 ...