HDU - 4725 The Shortest Path in Nya Graph(最短路+虚点操作)

problem

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 <= 10 5) and C(1 <= C <= 10 3), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers l i (1 <= l i <= N), which is the layer of i th node belong to.
Then come M lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 10 4), 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.

Examples

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

题目大意:
给定n个点,在一般的基础上给每个点一个维度,也就是多了一个参数表示所在的层次,现在已知:

1、X层的点可以通过增加权值C到达X-1层或X+1层中的任何一个点
2、给定m个已经形成的路径

将层转换理解为虚点,N开双倍,2e5。

这里要注意虚点操作时,过渡的边应该这么建立:
1、因为X层的点 i 通过增加权值C才能到达X-1层或X+1层
所以sz[i].push_back(edge(t+n-1,c));和sz[i].push_back(edge(t+n+1,c));
2、假设点 j 已经通过权值C到达X层,所以他可以到达X层所有点,即X层表示的虚点到X层的所有点的距离为0。所以sz[t+n].push_back(edge(i,0));

不要搞反了,这点很重要,因为涉及到你是否理解这题的虚点操作。

下面这个代码我为了省事,用cin,实际题面会T,用scanf或输入优化就可以过了。

#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int maxn=2e5+7;
struct edge{
	int to,w;
	edge(int a,int b){	to=a;	w=b;	}
};
vector<edge>sz[maxn];
int d[maxn],T,n,m,c;
bool vis[maxn];
void spfa(){
	memset(vis,0,sizeof(vis));	memset(d,inf,sizeof(d));
	d[1]=0;	vis[1]=1;
	queue<int>q;	q.push(1);
	while(!q.empty()){
		int t=q.front();	q.pop();	vis[t]=0;
		for(int i=0;i<sz[t].size();i++){
			int y=sz[t][i].to;
			int x=sz[t][i].w;
			if(d[y]>d[t]+x){
				d[y]=d[t]+x;
				if(!vis[y]){
					vis[y]=1;
					q.push(y);
				}
			}
		}
	}
}
int main(){
	cin>>T;
	for(int tt=1;tt<=T;tt++){
		cin>>n>>m>>c;
		for(int i=1;i<=2*n;i++)	sz[i].clear();
		for(int i=1;i<=n;i++){
			int t;	cin>>t;
			sz[t+n].push_back(edge(i,0));
			if(t>1)	sz[i].push_back(edge(t+n-1,c));
			if(t<n)	sz[i].push_back(edge(t+n+1,c));
		}
		for(int i=1;i<=m;i++){
			int a,b,c;
			cin>>a>>b>>c;
			sz[a].push_back(edge(b,c));
			sz[b].push_back(edge(a,c));
		}
		spfa();
//		for(int i=1;i<=n;i++)	cout<<d[i]<<' ';
//		putchar('\n');
		if(d[n]==inf)	d[n]=-1;
		printf("Case #%d: %d\n",tt,d[n]);
	}
}
/*
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

Case #1: 2
Case #2: 3
*/

可以看其他人的题解:链接,还有图解

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
All-Pairs Shortest Path问题是指在一个带权有向图中,求出任意两个节点之间的最短路径。解决这个问题的算法称为All-Pairs Shortest Path算法。 常用的All-Pairs Shortest Path算法有Floyd-Warshall算法和Johnson算法。 Floyd-Warshall算法的基本思想是动态规划。用dist[i][j]表示从节点i到节点j的最短路径长度,用k表示中间节点,则有状态转移方程: ``` dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) ``` 其中,dist[i][j]的初始值为节点i到节点j的边权,如果i和j之间没有边,则为正无穷。算法的核心是对k从1到n的循环,依次更新dist[i][j]的值,最终得到所有节点之间的最短路径长度。 Floyd-Warshall算法的时间复杂度为O(n^3),其中n为节点数,主要时间花费在三层循环上,实际应用中可以通过空间换时间的方式优化算法。 Johnson算法的基本思想是通过引入一个拟节点,并将其与所有节点之间的边权设为0,将问题转化为带权有向图中的单源最短路径问题。然后使用Bellman-Ford算法求出拟节点到其它所有节点的最短路径长度,再用求最短路径时的松弛操作更新所有边的边权,将问题转化为带权有向图中的多源最短路径问题。最后使用Dijkstra算法求出所有节点之间的最短路径长度。 Johnson算法的时间复杂度为O(n^2logn+m),其中n为节点数,m为边数,主要时间花费在Bellman-Ford算法和Dijkstra算法上,实际应用中可以通过优化数据结构等方式优化算法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值