NC219649. SlowLeak
描述
输入描述
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; }