题意:给你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(最短路建边)题解的更多相关文章

  1. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. (中等) 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 ...

  8. 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 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. webpack中,require的五种用法

    a.js: module.exports = function(x){ console.log(x); } 一,commonjs同步: var b = require('./a');b('你好')// ...

  2. 高中生的IT之路-1.3那一幕

    上一篇讲到,当时我认为自己的命运就是小时候上学,长大后外出打工,所以高中毕业后就来到了天津,到爸爸的店铺打工. 我爸的店铺就在天津大学校园里,幸运的是,我人生的转折点也就在此. 刚到店里那段时间,每天 ...

  3. Android DatepickerDialog(日期选择器)的使用

    效果图如下: 日期和时间选择对话框,首先是要获得当前时间,这里用 java类中的Calendar来获得日期和时间(也可以用Date,但是不提倡,Date部分方法已经注释为过时), Calendar是一 ...

  4. 移动端开发--rem和像素如何使用!

    刚开始做移动端的开发的时候,大多时候是在使用 像素(PX)来写页面和布局的,但是使用下来,还是有多少不好用! 随之就开始用 rem 来写,rem写的结果是页面放到一些屏幕上,字体过小,如果页面渲染用了 ...

  5. poj1934 Trip【线性DP】【输出方案】

    Trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3850   Accepted: 1030 Description ...

  6. mongoexport

    导数据 数据同步 mongodb无自增id 数据断点 mongoexport — MongoDB Manual https://docs.mongodb.com/manual/reference/pr ...

  7. linux内存管理之vmalloc函数分析

    2017-07-09 今天周末,闲来无事聊聊linux内核内存分配那点事……重点在于分析vmalloc的执行 流程 以传统x86架构为例,内核空间内存(3G-4G)主要分为三大部分:DMA映射区,一致 ...

  8. sql server 驱动程序在 \Device\RaidPort0 上检测到控制器错误。

    sql server 驱动程序在 \Device\RaidPort0 上检测到控制器错误. 错误情况,如下图: 原因分析:硬盘故障 解决办法:进行迁移

  9. Winzip和Winrar命令行的使用

    Winzip和Winrar除了提供丰富灵活的图形界面操作功能外,二者都还具备从命令行模式进行文件的压缩与解压缩操作功能,极大的方便了我们在批处理文件中调用压缩软件.以下给出了二者的常用命令行使用方法: ...

  10. ES6基本语法和一些面向对象的知识

    声明变量 var 使用var声明变量会将变量的声明提到全局,在局部作用域声明的在全局也能打印 { var a = 12; } // 变量提升 var会将变量的声明提到全局 console.log(a) ...