1111. Online Map

#include <stdio.h>
#define MaxVNum 500
#define INF 10000
void dj(int l[][MaxVNum],int t[][MaxVNum],int path[], int S,int dist[],int n,int time[]);
void dj2(int t[][MaxVNum], int path[], int S, int time[], int n, int cnt[]);
int FindMinDist(int n, int collected[], int dist[]);
void PrintPath(int path[], int D,int S);
int main()
{
	int t[MaxVNum][MaxVNum],l[MaxVNum][MaxVNum];
	int n, m, oneway,i,v1,v2,S,D,dist1[MaxVNum],path1[MaxVNum],path2[MaxVNum],time1[MaxVNum],cnt[MaxVNum],time2[MaxVNum];
	scanf("%d %d", &n, &m);
	for (v1 = 0; v1 < n; v1++)
		for (v2 = 0; v2 < n; v2++)
			l[v1][v2] = t[v1][v2] = INF;
	for (i = 0; i < m; i++) {
		scanf("%d %d %d", &v1, &v2,&oneway);
		scanf("%d %d", &l[v1][v2], &t[v1][v2]);
		if (!oneway) {
			l[v2][v1] = l[v1][v2];
			t[v2][v1] = t[v1][v2];
		}
	}
	scanf("%d %d", &S, &D);
	dj(l,t,path1,S, dist1,n,time1);	
	dj2(t,path2, S, time2,n,cnt);

	for (v2 = v1 = D;v1 == v2&&v1 != -1; v1 = path1[v1], v2 = path2[v2])
		;
	if (v1 == -1 && v2 == -1) {
		printf("Distance = %d; Time = %d: ", dist1[D],time1[D]);
		PrintPath(path1, D, S);
	}
	else {
		printf("Distance = %d: ", dist1[D]);
		PrintPath(path1, D,  S);
		printf("\n");
		printf("Time = %d: ", time2[D]);
		PrintPath(path2, D, S);
	}
	

	
	return 0;
}

void dj(int l[][MaxVNum], int t[][MaxVNum], int path[], int S, int dist[], int n,int time[])
{
	int v,w;
	int collected[MaxVNum];
	for (v = 0; v < n; v++) {
		collected[v] = 0;
		dist[v] = l[S][v];
		time[v] = t[S][v];
		path[v] = -1;
	}
	dist[S] = 0;
	time[S] = 0;
	collected[S] = 1;
	while (1) {
		v = FindMinDist(n,collected, dist);
		if (v == -1)
			return;
		collected[v] = 1;
		for (w = 0; w < n; w++) {
			if ((!collected[w]) && (l[v][w] < INF))
				if (dist[v] + l[v][w] < dist[w]) {
					dist[w] = dist[v] + l[v][w];
					time[w] = time[v] + t[v][w];
					path[w] = v;
				}
				else if (dist[v] + l[v][w] == dist[w] && time[v] + t[v][w] < time[w]) {
					time[w] = time[v] + t[v][w];
					path[w] = v;
				}
		}
	}
}

int FindMinDist(int n, int collected[], int dist[])
{
	int MinV, V;
	int MinDist = INF;

	for (V = 0; V<n; V++) {
		if (!collected[V] && dist[V]<MinDist) {
			MinDist = dist[V];
			MinV = V;
		}
	}
	if (MinDist < INF)
		return MinV;
	else return -1;
}

void PrintPath(int path[], int D,int S)
{
	if (D != -1) {
		PrintPath(path, path[D], S);
		printf(" -> %d",D);
	}
	else
		printf("%d", S);

}

void dj2(int t[][MaxVNum], int path[], int S, int time[], int n, int cnt[])
{
	int v, w;
	int collected[MaxVNum];
	for (v = 0; v < n; v++) {
		collected[v] = 0;
		time[v] = t[S][v];
		path[v] = -1;
		cnt[v] = INF;
	}
	time[S] = 0;
	cnt[S] = 0;
	collected[S] = 1;
	while (1) {
		v = FindMinDist(n, collected, time);
		if (v == -1)
			return;
		collected[v] = 1;
		for (w = 0; w < n; w++) {
			if ((!collected[w]) && (t[v][w] < INF))
				if (time[v] + t[v][w] < time[w]) {
					time[w] = time[v] + t[v][w];
					cnt[w] = cnt[v] + 1;
					path[w] = v;
				}
				else if (time[v] + t[v][w] == time[w] && cnt[w] > cnt[v] + 1) {
					cnt[w] = cnt[v] + 1;
					path[w] = v;
				}
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值