最短路 加油站

两遍Flody

第一遍全体遍历

第二遍把1,n,加油站 拿出来遍历一遍

You are an avid cyclist and bike every day between your home and school. Usually, your ride is uneventful and you bike along the shortest path between home and school. After school this afternoon you realized you have a slow leak in your bike tire——the tire can hold some air, but not for long. Refilling the tire allows you to ride your bike for some distance, after which your tire goes flat again and it becomes impossible to ride any further (and you refuse to walk your bicycle).

Luckily for you, your city has installed several bike repair stations at intersections throughout town where you can refill your tire and bike again until the tire goes flat. There's a repair station at your school too, so that you can fill up your tire before you start on your trip home.

You've calculated how far you can bike before your tire runs out of air and you know the layout of your town, including all the intersections, distances between them, and the locations of the repair stations. What is the shortest possible trip from school to your home that you can take without becoming stuck due to a flat tire? (You do not become stuck if you roll into a repair station, or your home, at the exact same time as your tire goes flat.)

输入描述:
The first line of input contains four integers nn, mm, tt, and dd, satisfying 2≤n≤500,0<m≤n∗(n−1)/2, 0 < t < n and 0 < d < 2^{31}
. The value nn represents the number of intersections in the city, mm represents the number of roads in the city, tt is the number of repair stations and dd is the distance that you can bike (starting with a fully inflated tire) before your tire goes flat again. The intersections are numbered from 11 to nn. Your school is at intersection 11 and your home is at intersection nn.

The second line of input contains tt integers, with values between 22 and n − 1n−1, inclusive. These are the intersections where the repair stations are located (excluding your school's repair station). All integers on this line are unique.

The next mm lines represent the roads in your town. Each has three integers i, j, ki,j,k, where 1≤i<j≤n, and 0 < k < 2^{31}
. These three integers represent that there is a direct road from intersection ii to intersection jj of length kk. Roads can be traveled in either direction. There is at most one road between any two intersections.

输出描述:
Print the minimum total distance you need to travel to reach home from school without getting stuck due to a flat tire. If the trip is not possible, output the word stuck on a single line instead.

It is guaranteed that if the trip is possible, the minimum distance DD satisfies 0 < D < 2^{31}

示例1

输入
6 7 2 4
2 5
1 2 4
1 3 5
2 3 3
3 4 2
3 5 1
4 6 4
5 6 4
输出
12


示例2
输入
6 7 2 10
2 5
1 2 1
1 3 2
2 3 1
3 4 8
4 5 3
4 6 2
5 6 1
输出
stuck

题解:

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
long long dis[510][510];
vector<int>v;
int main()
{
	memset(dis, 0x3f, sizeof(dis));
    long long INF=dis[0][0];
	int n, m, k, D;
	scanf("%d%d%d%d",&n,&m,&k,&D);
	for (int i = 0; i < k; i++)
	{
		int t;
		scanf("%d", &t);
		v.push_back(t);
	}
	v.push_back(1);
	v.push_back(n);
	for (int i = 1; i <= n; i++)
		dis[i][i] = 0;
	for (int i = 1; i <= m; i++)
	{
		int a, b,d;
		scanf("%d%d%d", &a, &b, &d);
		dis[a][b] = dis[b][a] = d;
	}
	for (int k = 1; k <= n; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				dis[i][j] =dis[j][i]= min(dis[i][j], dis[i][k] + dis[k][j]);
	for (int i = 0; i < v.size(); i++)
		for (int j = 0; j < v.size(); j++)
			if (dis[v[i]][v[j]] > D)
				dis[v[i]][v[j]] = INF;
	for (int k = 0; k < v.size(); k++)
		for (int i = 0; i < v.size(); i++)
			for (int j = 0; j < v.size(); j++)
				dis[v[i]][v[j]] =dis[v[j]][v[i]]= min(dis[v[i]][v[j]], dis[v[i]][v[k]] + dis[v[k]][v[j]]);
	if (dis[1][n] >= INF)
		cout << "stuck";
	else
		 printf("%lld\n",dis[1][n]);
	return 0;
}
posted @ 2021-03-22 19:29  你看码!!!  阅读(53)  评论(0)    收藏  举报