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 ...
 
随机推荐
- Nginx搭建详细
			
Linux 安装Nginx搭建详细内容 进入:/usr/java/nginx位置下载nginx: wget et http://nginx.org/download/nginx-1.8.0.tar.g ...
 - Android开发——通过wifi接收IPCamera视频流
			
前面,我们已经了解了怎么在android app上打开关闭和扫描,搜索wifi,现在,我来写一下怎么通过连接wifi来使app获取到IPCamera摄像头的视频. 一.通过URL获取视频的地址 二.创 ...
 - Android Studio项目/Flutter 案例中Gradle报错通用解决方案(包括Unable to tunnel through proxy问题)
			
目录 Step 1:修改Gradle版本为本地版本 Step 2:修改classpath为Android Studio版本 Step 3:关闭代理 Step 1:修改Gradle版本为本地版本 ...
 - 测试通过mweb进行发布Title
			
MWeb 是专业的 Markdown 写作.记笔记.静态博客生成软件,目前已支持 Mac,iPad 和 iPhone.MWeb 有以下特色: 软件本身: 使用原生的 macOS 技术打造,追求与系统的 ...
 - React 如何搭建脚手架
			
React 如何搭建脚手架 npm install -g create-react-app //安装 create-react-app react-demo // react-demo ...
 - Spring cloud 超时配置总结
			
基准配置: eureka-server : 注册中心 端口号1000 service-A : 服务A端口号2000 service-B : 服务B 端口号3000 其中,B服务通过feign调用服务A ...
 - No!No!No! It's not fashion!
			
还记得搞怪的hold住姐Miss Lin么,对于人们常规的行为,Miss Lin会挑起夸张的眉毛说:"Oh my God, it's not fashion!".如果程序员圈子里有 ...
 - Java——字符串
			
1.不可变的String String对象是不可变的.String类中的每一个看起来会修改String值的方法,实际上都是创建了一个全新的String对象,以包含修改修改后的字符串内容. public ...
 - 解决OneNote同步出错
			
问题: onenote同步出现黄色叹号. 解决: 分析: 对每个分区进行设置密码,不能设置的证明该分区有问题.(可能不只一个分区卡同步) 解决方法: 1,将有问题的分区分制一份,然后删掉原来的分区 2 ...
 - jenkins增量更新及重启服务步骤
			
jenkins增量更新步骤:(以creditsys_service_tomcat为例) 1.SecureCRT 或者Xshell 连接服务器192.168.*.*,账号:test/**** 2.cd ...