[24] Choose the best route

题意:见上图。(用翻译插件翻译了一下)是一个多起点单终点的问题。

算法:digkstra

问题:以为是双向,结果wa了;改成单向过了,虽然不知道为什么是单向。

思路: 设置一个源起点连接数个起点,设置长度为0,转化为普通问题求解。

代码:ac

#include<bits/stdc++.h>
using namespace std;
#define inf 0x7fffffff
int Map[1010][1010];
int vis[1010];
int Dist[1010];
int n, m;
int s, e;
int p, q, t;
int w;
int i, j;
int Min;
int nex;

int main()
{
	while (scanf("%d%d%d", &n, &m, &e)!=EOF) {
		for (i = 0; i <= n; i++) {
			Dist[i] = inf;
			vis[i] = 0;
			for (j = 1; j <= n; j++)
				Map[i][j] = inf;
		}
		while (m--) {
			scanf("%d%d%d", &p, &q, &t);
			Map[p][q] = min(Map[p][q], t);
		}
		s = 0;
		scanf("%d", &w);
		while (w--) {
			int temp;
			scanf("%d", &temp);
			Map[s][temp]= 0;
		}
		vis[s] = 1;
		Dist[s] = 0;
		while (s != e) {
			Min = inf;
			for (i = 1; i <= n; i++) {
				if (Map[s][i] != inf)
					Dist[i] = min(Dist[i], Dist[s] + Map[s][i]);
				if (!vis[i] && Dist[i] < Min) {
					nex = i;
					Min = Dist[nex];
				}
			}
			if (Min == inf)break;
			s = nex;
			vis[nex] = 1;
		}
		if (Dist[e] == inf)printf("-1\n");
		else printf("%d\n", Dist[e]);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值