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 (Java/Others)
Total Submission(s): 13445    Accepted Submission(s): 2856

 

Problem Description

This is a very easy problem, your task is just calculate el camino mas corto
en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do
not understand a word of this paragraph, just move on.
The Nya graph is an undirected graph with "layers". Each node in the graph
belongs to a layer, there are N nodes in total.
You can move from any node in layer x to any node in layer x + 1, with cost
C, since the roads are bi-directional, moving from layer x + 1 to layer x is
also allowed with the same cost.
Besides, there are M extra edges, each connecting a pair of node u and v,
with cost w.
Help us calculate the shortest path from node 1 to node N.

 

Input

The first line has a number T (T <= 20) , indicating the number of test
cases.
For each test case, first line has three numbers N, M (0 <= N, M <= 105)
and C(1 <= C <= 103), which is the number of nodes, the number of
extra edges and cost of moving between adjacent layers.
The second line has N numbers li (1 <= li <= N), which
is the layer of ith node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w
(1 <= w <= 104), which means there is an extra edge, connecting a
pair of node u and v, with cost w.

 

Output

For test case X, output "Case #X: " first, then output the minimum cost
moving from node 1 to node N.
If there are no solutions, output -1.

 

Sample Input

 

2 3 3 3 1 3 2 1 2 1 2 3 1 1 3 3 3 3 3 1 3 2 1 2 2 2 3 2 1 3 4

 

 

Sample Output

 

Case #1: 2 Case #2: 3

 

 

Source

2013 ACM/ICPC Asia Regional Online —— Warmup2

 

Recommend

zhuyuanchen520


【题意】

给一张图,n个点,m条有权无向边,每个点属于某一层,相邻层间的任意两点存在一条权值为C的边,问1到n的最短路


【分析】

此图数据量比较大,暴力建图不可取!会MLE or TLE.

可以将层也抽象化成点,也就是一共有N个点节点和N个层节点,然后按照层与层之间(双向,权值C)、点与点之间(即后来给的M条边)、点与相对应的层之间(层指向点,权值0),点与对应层的相邻层之间(点指向层,权值C)建图,最后求最短路即可

解释一个代码中难理解的地方:

这里是点和所在层建立关系
 不能建双向边的原因是假设有两个点在同一层

比如有三个点,点1在第一层,点2也在第一层,虚拟第一层为点4,那么1-4有一条距离为0的点,4-1有一条距离为0的点

2-4有一条距离为0的点,4-2有一条距离为0的点,那么1-2距离就成为0了,这是不对的。

这两个if建立单向边的原因是,如果三层,中间一层没有点,建立双向边会导致最上和最下的两层可以相通,而事实上是不通的


【代码】

#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=2e5+5;
#define pir pair<int,int>
int n,m,c,cas,cnt,S,la[N],dis[N];bool has[N];
struct node{int v,w,next;}e[N<<2];int tot,head[N];bool vis[N];
inline void addedge(int x,int y,int z){
	e[++tot].v=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot;
}
inline void add(int x,int y,int z){
	addedge(x,y,z);
	addedge(y,x,z);
}
inline void Clear(){
	tot=0;
	memset(head,0,sizeof head);
	memset(has,0,sizeof has);
	memset(vis,0,sizeof vis);
	memset(dis,0x3f,sizeof dis);
}
inline void Init(){
	scanf("%d%d%d",&n,&m,&c);
	for(int i=1;i<=n;i++) scanf("%d",&la[i]),has[la[i]]=1;
	for(int i=1,x,y,z;i<=m;i++) scanf("%d%d%d",&x,&y,&z),add(x,y,z);
	for(int i=1;i<=n;i++) if(has[i]&&has[i+1]) add(i+n,i+1+n,c);
	for(int i=1;i<=n;i++){
		addedge(la[i]+n,i,0);
		if(la[i]>1) addedge(i,la[i]+n-1,c);
		if(la[i]<n) addedge(i,la[i]+n+1,c);
	}
}
#define mp make_pair
inline void dijkstra(){
	priority_queue<pir,vector<pir>,greater<pir> >q;
	q.push(mp(dis[S=1]=0,S));//vis[S]=1;
	while(!q.empty()){
		pir t=q.top();q.pop();
		int x=t.second;
		if(vis[x]) continue;
		vis[x]=1;
		for(int i=head[x];i;i=e[i].next){
			int v=e[i].v;
			if(!vis[v]&&dis[v]>dis[x]+e[i].w){
				q.push(mp(dis[v]=dis[x]+e[i].w,v));
			}
		}
	}
	printf("Case #%d: %d\n",++cnt,dis[n]<0x3f3f3f3f?dis[n]:-1);
}
int main(){
	for(scanf("%d",&cas);cas--;){
		Clear();
		Init();
		dijkstra();
	}
	return 0;
} 

原文地址:https://www.cnblogs.com/shenben/p/10420844.html

时间: 2024-11-03 17:54:09

HDU 4725 The Shortest Path in Nya Graph(构图)的相关文章

hdu 4725 The Shortest Path in Nya Graph(建图+优先队列dijstra)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:有n个点和n层,m条边,每一层的任意一个点都可以花费固定的值到下一层或者上一层的任意点 然后m条边链接的点可以花费给出的值进行转移,最后问从i点到n点最少要花费多少. 这题点的个数有100000,层数也是100000,不算额外边暴力建边肯定要爆. 所以可以把层数也当成一个点比如说i点在j层于是n+j就与i链接花费0然后i点可以和上下层任意一个点链接 及i与n+j+1链接i与n+j-1链接

The Shortest Path in Nya Graph HDU - 4725

Problem Description This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on.The Nya graph is an un

HDU - 4725 (The Shortest Path in Nya Graph)层次网络

题意:有n个点,每个点都在一个层内,层与层之间的距离为c,一个层内的点可以到达与它相邻的前后两个层的点,还有m条小路 ..时间真的是卡的很恶心啊... 借一下别人的思路思路: 这题主要难在建图上,要将层抽象出来成为n个点(对应编号依次为n+1~n+n),然后层与层建边,点与点建边,层与在该层上的点建边(边长为0),点与相邻层建边(边长为c). ps:这样处理就不用拆点了.不过要注意的是相邻两层必须都要有点才建边(不然会WA,可以参考我贴的数据) 借鉴自:http://www.myexceptio

HDU4725 The Shortest Path in Nya Graph(堆优化的dijkstra算法)

题意: 这是一个非常容易解决的问题,您的任务只是计算图像,而仅是计算干草成本和算法成本.如果您不懂此段话,请继续.Nya图是具有“层”的无向图.图中的每个节点都属于一个层,总共有N个节点.您可以以成本C从x层中的任何节点移动到x + 1层中的任何节点,因为道路是双向的,因此也可以以相同的成本从x + 1层移动到x层.此外,还有M个额外的边,每个边连接一对节点u和v,成本为w.帮助我们计算从节点1到节点N的最短路径. 题解: 主要是建图. N个点,然后有N层,要假如2*N个点. 总共是3*N个点.

HDU - 2290 Find the Path(最短路)

HDU - 2290 Find the Path Time Limit: 5000MS   Memory Limit: 64768KB   64bit IO Format: %I64d & %I64u Submit Status Description Scofield is a hero in American show "Prison Break". He had broken the prison and started a big runaway. Scofield h

[LeetCode] 847. Shortest Path Visiting All Nodes 访问所有结点的最短路径

An undirected, connected graph of N nodes (labeled?0, 1, 2, ..., N-1) is given as?graph. graph.length = N, and?j != i?is in the list?graph[i]?exactly once, if and only if nodes?i?and?j?are connected. Return the length of the shortest path that visits

Dijkstra’s Shortest Path Algorithm / LeetCode 787. Cheapest Flights Within K Stops

Dijkstra’s Shortest Path Algorithm 实现详见:https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-using-priority_queue-stl/ 需要注意的是,priority_queue并无法更新内部的元素,因此我们更新dist的同时,直接把新的距离加入pq即可.pq里虽然有outdated的dist,但是由于距离过长,他们并不会更新dist. // If there is sho

6-17 Shortest Path [4] (25分)

Write a program to find the weighted shortest distances from any vertex to a given source vertex in a digraph. If there is more than one minimum path from v to w, a path with the fewest number of edges is chosen. It is guaranteed that all the weights

Method for finding shortest path to destination in traffic network using Dijkstra algorithm or Floyd-warshall algorithm

A method is presented for finding a shortest path from a starting place to a destination place in a traffic network including one or more turn restrictions, one or more U-turns and one or more P-turns using a Dijkstra algorithm. The method as sets a