HDU-4725 The Shortest Path in Nya Graph (拆点+dji)
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)的更多相关文章
- HDU - 4725 The Shortest Path in Nya Graph(拆点+Dijkstra)
题意:N个点,每个点有一个层号L,相邻的两层 Li 与 Li+1 之间的距离为C.另外给出M条无向边,求从点1到点N的最短路. 分析:同一层之间的两点距离并不是0,这是一个小坑.依次把相邻两层的所有点 ...
- 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 ...
随机推荐
- ESP-8266 RTOS 环境搭建
本节为 ESP-8266 RTOS 的环境搭建 只适合Linux环境,推荐Ubuntu.本例以Ubuntu16.04-x64为例 安装 git [dzlua@ubuntu: ~]$ sudo apt ...
- codeforces 213div(2) 365 A.Good Number 365 B.The Fibonacci Segment
#include <stdio.h> #include <string.h> bool vis[11]; int n, k; bool judge(int x) { memse ...
- linux下pip的安装
---恢复内容开始--- 1 输入apt-cache search wxpython 如果有返回信息 则输入 sudo apt-get install python-tools 2 否则 1.添加软件 ...
- IO流与NIO流
JAVA IO流最详解 (转自CSDN) IO流上:概述.字符流.缓冲区(java基础) 一.IO流概述 概述: IO流简单来说就是Input和Output流,IO流主要是用来处理设备之间的数 ...
- git的使用学习笔记
一.git Git 是一个开源的分布式版本控制系统,项目版本管理工具,可以在本地提交修改再合并到主分支上,最为出色的是它的合并跟踪(merge tracing)能力. 可以通过Linux命令进行增加, ...
- Django中自定义admin---Xadmin的实现
在Django框架中,自带一个后台管理页面admin,这个管理页面很全,但是,有些并不是我们需要的,所以我们可以根据admin的实现流程来自定义自己的需求,即根据admin的实现方式来实现自定制--X ...
- GBK和UTF-8的区别
我们这里将以最简单最容易理解的方式来描述GBK和UTF8的区别,以及它们分别是什么. GBK编码:是指中国的中文字符,其它它包含了简体中文与繁体中文字符,另外还有一种字符“gb2312”,这种字符 ...
- android ——Intent
Intent是android程序中各组件之间进行交互的重要方式,它可以用于指明当前组件想要执行的动作,也可以在不同组件之间传递数据,Intent一般被用于启动活动,启动服务以及发送广播. 一.显式的使 ...
- Spring入门(七):Spring Profile使用讲解
1. 使用场景 在日常的开发工作中,我们经常需要将程序部署到不同的环境,比如Dev开发环境,QA测试环境,Prod生产环境,这些环境下的一些配置肯定是不一样的,比如数据库配置,Redis配置,Rabb ...
- 关于int的范围以及溢出问题
最近在练一些算法题目的时候恰巧碰到了几道关于int范围与溢出相关的问题,于是就整理一下. 1.原码.补码 在计算机中数值都是用补码表示和存储的(正数补码与原码一致,负数补码是原码符号位不变,其余位取反 ...