PAT 1003

单源最短路径的算法,存在 相等长度的路径,再做额外判断即可。

#include <bits/stdc++.h>
#define MAX 1000000
using namespace std;
int n,m,a,b;
int G[501][501];
int resc[501];
int num = 1;
int dis[501];
int visited[501] = {0};
int totalresc[501];
int pre[501];
int FindMin(){
	int MinDist = MAX,MinV = -1;
	for(int i = 0; i < n; i++)
		if(!visited[i] && dis[i] < MinDist){
			MinDist = dis[i];
			MinV = i;
		}
	return MinV;
}
void dijkstra(){
	for(int i = 0; i <n; i++){
		dis[i] = G[a][i];
		if(G[a][i] != MAX){
			totalresc[i] = resc[a] + resc[i];
			pre[i] = 1;
		}
	 	else{
	 		totalresc[i] = resc[i];
	 		pre[i] = 0;
		}
	}
	dis[a] = 0;
	pre[a] = 1;
	visited[a] = 1;
	totalresc[a] = resc[a];
	while(1){
		int nod = FindMin();
		if(nod == -1) break;
		visited[nod] = 1;
		for(int i = 0; i < n; i++){
			if(!visited[i])
				if(dis[nod] + G[nod][i] < dis[i]){
					dis[i] = dis[nod] + G[nod][i];
					totalresc[i] = totalresc[nod] + resc[i];
					pre[i] = pre[nod];
					
				}
				else if(dis[nod] + G[nod][i] == dis[i]){
					pre[i] = pre[i] + pre[nod];
					if(totalresc[nod] + resc[i] > totalresc[i])
					totalresc[i] = totalresc[nod] + resc[i];
			    }
		}
	}
}
int main(){
	//freopen("1.txt","r",stdin);
	int x,y,l;
	cin >> n >> m >> a >> b;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++) G[i][j] = MAX;
	for(int i = 0; i < n; i++) cin >> resc[i];
	while(m--){
		cin >> x >> y >> l;
		G[x][y] = l;
		G[y][x] = l;
	}
	dijkstra();
	cout << pre[b] << " " << totalresc[b];
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值