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. 【iOS】[[NSBundle mainBundle] loadNibNamed:nibTitle owner:self options:nil] 异常

    这两天照书上的例子写代码时,出现了这个异常. 上网查了不少,有人说链接失效什么的……但发现都不是那些原因,问题出现在下面这句代码: [[NSBundle mainBundle] loadNibName ...

  2. 【Android Studio】Frameworks detected: Android framework is detected in the project Configure

    刚开始在 Mac 上用 Android Studio, 打开第一个项目就遇到了问题,描述如下: 上午9:: Frameworks detected: Android framework is dete ...

  3. 【iOS】Receiver type 'XXX' for instance message is a forward declaration

    今天遇到这个错误.刚开始字体太大,没显示全,后来调小字体之后看到了完整提示信息: 之后就忽然想起没引入相关的类,添加 #import "RDVTabBarItem.h" 就行了.

  4. codeforces 347A - Difference Row

    给你一个序列,让你求(x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn).值最大的一个序列,我们化简一下公式就会发现(x1 - x2) + (x2 - x3) + . ...

  5. 从js 讲解时间复杂度和空间复杂度

    1. 博客背景 今天有同事在检查代码的时候,由于函数写的性能不是很好,被打回去重构了,细思极恐,今天和大家分享一篇用js讲解的时间复杂度和空间复杂度的博客 2. 复杂度的表示方式 之前有看过的,你可能 ...

  6. 章节十五、6-log4 2-用默认的配置

    一.实例演示 package log4jtutorial; import org.apache.logging.log4j.LogManager; import org.apache.logging. ...

  7. java+springBoot+Thymeleaf+vue分页组件的定义

    导读 本篇着重介绍java开发环境下,如何写一个vue分页组件,使用到的技术点有java.springBoot.Thymeleaf等: 分页效果图 名称为vuepagerbasic的分页组件,只包含上 ...

  8. WinForm开发中通用附件管理控件设计开发参考

    1.引言 在WinForm开发中,文件附件的管理几乎在任何一个应用上都会存在,是一个非常通用集中的公共模块.我们日常记录会伴随着有图片.文档等附件形式来展现,如果为每个业务对象都做一个附件管理,或者每 ...

  9. 1和new Number(1)有什么区别

    1和new Number(1)有什么区别 author: @Tiffanysbear 总结,两者的区别就是原始类型和包装对象的区别. 什么是包装对象 对象Number.String.Boolean分别 ...

  10. Activiti 开发案例之动态指派任务

    流程图 以上是一个请假的流程图,以下为流程任务节点描述: 员工发起请假流程 部门经理审批 同意则进入人事审批 拒绝则调整申请或者直接结束流程 人事审批通过则进入销假环节 人事审批拒绝则调整申请或者直接 ...