题意:给你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. OC开发_Storyboard——UIApplication和网络活动指示器

    一.UIApplication 只有一个实例: UIApplication *myApplication = [UIApplication sharedApplication]; 属性如果设置为YES ...

  2. 三维凸包求重心到面的最短距离(HDU4273)

    http://acm.hdu.edu.cn/showproblem.php?pid=4273 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  3. nginx 重定向

    不带www跳转www 1.301: return 301 http://www.xx.com$request_uri; 2.(1)rewrite ^(.*)$ http://www.xx.com$1 ...

  4. Android数据存储之SQLite 数据库学习

    Android提供了五种存取数据的方式 (1)SharedPreference,存放较少的五种类型的数据,只能在同一个包内使用,生成XML的格式存放在设备中 (2) SQLite数据库,存放各种数据, ...

  5. MySQL实现根据当前ID读取上一条和下一条记录

    以下为MySQL语句演示: SELECT * FROM t_news AS n ORDER BY n.`News_ID` 当前ID为4,读取上一条记录: ,; 当前ID为4,读取下一条记录: ,; 其 ...

  6. java 常见几种发送http请求案例

    import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java ...

  7. Fatal error in launcher: Unable to create process using '"c:\python37\python3.exe" "C:\Python37\Scripts\pip3.exe" install opencv-python'

    pip3.exe install opencv-python 报错: Fatal error in launcher: Unable to create process using '"c: ...

  8. Andrew Ng机器学习公开课笔记 -- Generalized Linear Models

    网易公开课,第4课 notes,http://cs229.stanford.edu/notes/cs229-notes1.pdf 前面介绍一个线性回归问题,符合高斯分布 一个分类问题,logstic回 ...

  9. Drainage Ditches---hdu1532(最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题意: 每次下雨的时候,农场主John的农场里就会形成一个池塘,这样就会淹没其中一小块土地,在这 ...

  10. Mybatis三剑客之mybatis-plugin

    搜索mybatis plugin并安装. 如果没有的话,就按照如下: 1. 简介     mybatis plugin作为一款优秀的mybatis跳转插件,比起free mybatis plugin插 ...