列表

详情


NC219649. SlowLeak

描述

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 , and , satisfying  and . The value  represents the number of intersections in the city,  represents the number of roads in the city,  is the number of repair stations and  is the distance that you can bike (starting with a fully inflated tire) before your tire goes flat again. The intersections are numbered from  to . Your school is at intersection  and your home is at intersection .

The second line of input contains  integers, with values between  and , 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  lines represent the roads in your town. Each has three integers , where , and . These three integers represent that there is a direct road from intersection  to intersection  of length . 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  satisfies .

示例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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++11) 解法, 执行用时: 324ms, 内存消耗: 3584K, 提交时间: 2021-03-24 23:23:36

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) 	for(ll i=1;i<=n;i++)
const int N=1e5+7;
const ll inf=0x3f3f3f3f3f;
ll n,m,t,d;
int a[555];
ll g[555][555];
int main()
{
	cin>>n>>m>>t>>d;
	rep(i,t)
		cin>>a[i];
	a[++t]=1,a[++t]=n;
	memset(g,inf,sizeof(g));
	rep(i,m)
	{
		int x,y; ll z;
		cin>>x>>y>>z;
		g[x][y]=g[y][x]=z;
	}
	rep(k,n)
	 rep(i,n)
	  rep(j,n)
	   g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
	rep(i,t)
	  rep(j,t)
	  {
	  	int x=a[i],y=a[j];
	  	if(g[x][y]>d) g[x][y]=inf;
	  }
	rep(x,t)
	 rep(y,t)
	  rep(z,t)
	  {
	  	int k=a[x],i=a[y],j=a[z];
	  	g[i][j]=min(g[i][j],g[i][k]+g[k][j]);	
	  }
	if(g[1][n]==inf) cout<<"stuck"<<endl;
	else cout<<g[1][n]<<endl;       
	  
	
	return 0;
}

上一题