HDU 4725 The Shortest Path in Nya Graph : http://acm.hdu.edu.cn/showproblem.php?pid=4725

题意:

  在一个图中跑最短路,这个图中的每一个点都有一个等级,每次都只能向高一个等级或低一个等级的点跑。问最短的距离。

思路:

参考/kuangbin

  这道题自己一开始直接按等级枚举,发现这样的复杂度是n*n的。需要更加合理的建图。考虑给每个等级建立两个点,一个用于进,一个用于出。

  第i层,入边到N+2*i-1, 出边从N+2*i 出来。(1<= i <= N)

  N + 2*i    到  N + 2*(i+1)-1 加边长度为C. 表示从第i层到第i+1层。

  N + 2*(i+1) 到 N + 2*i - 1 加边长度为C,表示第i+1层到第i层。

  如果点i属于第u层,那么加边 i -> N + 2*u 和    N + 2*u -1 -> i,  长度都为0。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = 3e5+;
int n,m,c; int dis[maxn];
vector<pii>g[maxn];
void dji(){
memset(dis,inf,sizeof(dis));
priority_queue<pii>que;
dis[] = ;
que.push(pii(,)); while(!que.empty()){
pii tmp = que.top();que.pop();
int u = tmp.se;
if(dis[u] < -*tmp.fi)continue;
for(int i=; i<g[u].size(); i++){
int v = g[u][i].fi;
if(dis[v] > dis[u] + g[u][i].se)
{
dis[v] = dis[u] + g[u][i].se;
que.push(pii(-*dis[v], v));
}
}
} }
int main(){
int t;scanf("%d" , &t);
for(int T = ; T<=t; T++){
scanf("%d%d%d", &n, &m, &c);
for(int i=; i<=*n; i++)g[i].clear();
for(int i=; i<=n; i++){
int x;scanf("%d", &x);
g[i].pb(pii(n+*x,));
g[n+*x-].pb(pii(i,));
}
for(int i=; i<n; i++){
g[n+*i].pb(pii(n+*(i+)-,c));
g[n+*(i+)].pb(pii(n+*i-,c));
} for(int i=; i<=m; i++){
int u,v,w;
scanf("%d%d%d", &u, &v, &w);
g[u].pb(pii(v,w));
g[v].pb(pii(u,w));
} dji();
printf("Case #%d: ",T);
if(dis[n] < inf)printf("%d\n", dis[n]);
else printf("-1\n");
} return ;
}

HDU 4725

HDU-4725 The Shortest Path in Nya Graph (拆点+dji)的更多相关文章

  1. HDU - 4725 The Shortest Path in Nya Graph(拆点+Dijkstra)

    题意:N个点,每个点有一个层号L,相邻的两层 Li 与 Li+1 之间的距离为C.另外给出M条无向边,求从点1到点N的最短路. 分析:同一层之间的两点距离并不是0,这是一个小坑.依次把相邻两层的所有点 ...

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

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

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

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

  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 (最短路+建图)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

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

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

随机推荐

  1. SQL Server 插入数据时自增长列如何指定数值

    SQL Server 表在导入数据时,有时需要将自增长列指定数值,来保证导入前后的数据完全一致,如何实现? SQL Server 提供了方法: SET IDENTITY_INSERT,允许将显式值插入 ...

  2. 【python-django后端开发】Redis缓存配置使用详细教程!!!

    官方查阅资料:https://django-redis-chs.readthedocs.io/zh_CN/latest/ 1. 安装django-redis扩展包 1.安装django-redis扩展 ...

  3. Java 性能优化(一)

    Java 性能调优(一) 1.衡量程序性能的标准 (1) 程序响应速度: (2) 内存占有情况: 2.程序调优措施 (1) 设计调优 设计调优处于所有调优手段 的上层,需要在软件开发之前进行.在软件开 ...

  4. WPF界面的异步后台加载

    private void Init()         {                     BackgroundWorker worker = new BackgroundWorker(); ...

  5. Resource 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  6. UnityShader之积雪效果

    积雪效果是比较简单的,只需要计算顶点法线方向和世界向上方向之间的点乘,将得到的值与预设的阀值比较,小于阀值为0,用这个值进行插值就OK了 代码: Shader "MyShader/SnowS ...

  7. Go中的fmt几种输出的区别和格式化方式

    在日常使用fmt包的过程中,各种眼花缭乱的print是否让你莫名的不知所措呢,更让你茫然的是各种格式化的占位符..简直就是噩梦.今天就让我们来征服格式化输出,做一个会输出的Goer. fmt.Prin ...

  8. java并发编程(九)----(JUC)CyclicBarrier

    上一篇我们介绍了CountDownlatch,我们知道CountDownlatch是"在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待",即CountDownL ...

  9. 28岁,转行学 IT 靠谱吗?

    前几天在知乎上,刷到这么一个问题 鉴于有不少人看了我的blog给我私信一些职业规划相关的问题,讨论很多的就是担心自己年龄是否还适合转行. 于是决定静心下来码了一篇回答, 同时搬到博客园来供大家消遣.. ...

  10. 算法之《图》Java实现

    数据结构之图 定义(百度百科) 图的术语表 无向图 深度优先搜索 广度优先遍历 有向图 路径问题 调度问题 强连通性 最小生成树(无向图) 最小生成树的贪心算法 加权无向图的数据结构 Kruskal算 ...