HDU 4725 The Shortest Path in Nya Graph(最短路建边)题解
题意:给你n个点,m条无向边,每个点都属于一个层,相邻层的任意点都能花费C到另一层任意点,问你1到n最小路径
思路:没理解题意,以为每一层一个点,题目给的是第i个点的层数编号。这道题的难点在于建边,如果用最朴素的相邻层所有点互相连接,那么可能有5*10^4连5*10^4,复杂度O(n^2)。这里我们用拆点(?大概),把每一层拆出一个点,作为每一层点和相邻层连接的中转站。这里要特别注意,同一层的点的距离不是0,所以我们建边不能全是无向边:
1.层与层无向边,权值C
2.层与同层点建单向边,权值0
2.点与相邻层单向边,权值C
这样,每个点都能通过每层拆出的点连接相邻层的点,而且同层的点的距离不为0。然而写完这些后我又TLE了...orz,把建边的vector邻接表改成手动建边,358ms过
代码:
#include<cstdio>
#include<set>
#include<cmath>
#include<stack>
#include<vector>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = +;
const int INF = 0x3f3f3f3f;
struct Edge{
int v,w,next;
}edge[*maxn];
bool vis[maxn];
int dis[maxn],head[maxn],tot;
void spfa(int start){
memset(vis,false,sizeof(vis));
memset(dis,INF,sizeof(dis));
vis[start] = true;
dis[start] = ;
queue<int> q;
while(!q.empty()) q.pop();
q.push(start);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u];i != -;i = edge[i].next){
int v = edge[i].v;
int w = edge[i].w;
if(dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
if(!vis[v]){
q.push(v);
vis[v] = true;
}
}
}
}
}
void addEdge(int u,int v,int w){
edge[tot].v = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
}
int layer[maxn];
bool have[maxn];
int main(){
int T;
int n,m,C,Case = ;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&C);
tot = ;
memset(head,-,sizeof(head));
memset(have,false,sizeof(have));
for(int i = ;i <= n;i++){
scanf("%d",&layer[i]);
have[layer[i]] = true;
}
for(int i = ;i < n;i++){ //层层建边
if(have[i] && have[i + ]){
addEdge(n + i,n + i + ,C);
addEdge(n + i + ,n + i,C);
}
}
for(int i = ;i <= n;i++){ //层点建边 相邻层点建边
addEdge(n + layer[i],i,);
if(layer[i] > )
addEdge(i,n + layer[i] - ,C);
if(layer[i] < n)
addEdge(i,n + layer[i] + ,C);
}
while(m--){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w);
addEdge(v,u,w);
}
spfa();
if(dis[n] == INF){
printf("Case #%d: -1\n",Case++);
}
else{
printf("Case #%d: %d\n",Case++,dis[n]);
}
}
return ;
}
HDU 4725 The Shortest Path in Nya Graph(最短路建边)题解的更多相关文章
- 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 ...
- 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,Dijkstra+加点。
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- HDU 4725 The Shortest Path in Nya Graph (最短路 )
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...
- HDU - 4725 The Shortest Path in Nya Graph 【拆点 + dijkstra】
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...
随机推荐
- TCP关闭连接(为什么会能Time_wait,Close_wait?)
版权声明:本文由胡文斌原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/102 来源:腾云阁 https://www.qclo ...
- hdu5411 CRB and Puzzle[矩阵优化dp]
CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- poj1568 Find the Winning Move[极大极小搜索+alpha-beta剪枝]
Find the Winning Move Time Limit: 3000MS Memory Limit: 32768K Total Submissions: 1286 Accepted: ...
- MACOS配置VIM成简单IDE傻瓜式操作
零.参考文献: https://www.jianshu.com/p/f0513d18742a 一.安照我的配置:保存文件 " Configuration file for vim " ...
- 【BZOJ1877】[SDOI2009]晨跑 最小费用最大流
[BZOJ1877][SDOI2009]晨跑 Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现 ...
- 表格table列宽度控制<colgroup>
<colgroup> 标签用于对表格中的列进行组合,以便对其进行格式化. 通过使用 <colgroup> 标签,可以向整个列应用样式,而不需要重复为每个单元格或每一行设置样式. ...
- poj3171 Cleaning Shifts【线段树(单点修改区间查询)】【DP】
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4422 Accepted: 1482 D ...
- python 的 ord()、 chr()、 unichr() 函数
一. ord() 函数描述ord() 函数是 chr() 函数(对于8位的ASCII字符串)或 unichr() 函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数,返 ...
- android进入页面会定位到ListView问题解决方法
在我们的页面中如果存在有ListView,当我们进入这个activity时,页面会定位到ListView的位置去,而不是activity的头部,这是由于ListView会去默认获取焦点所造成的. 解决 ...
- 【Maven学习】Nexus OSS私服仓库的备份与迁移
背景 在上一篇博客 [Maven学习]Nexus OSS私服仓库的安装和配置 中,我们已经在机房搭建好了新的Nexus OSS私服仓库.下面是两个版本的Nexus OSS私服仓库的对比图. 老的Nex ...