【LOJ #6075】「2017 山东一轮集训 Day6」重建(DP)
处理出表示走任意条边和沿着关键点走条边的最短路
然后枚举关键点边数
求出可行的的
对所有可行的取即可
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=1005,M=10005;
int n,m,str,des,k;
ll f[N][N],g[N][N],inf;
int vt[N];
vector<pii>e[N];
inline void calcf(){
f[0][des]=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(pii &x:e[j])
if(f[i][j]>f[i-1][x.fi]+x.se)f[i][j]=f[i-1][x.fi]+x.se;
}
inline void calcg(){
g[0][des]=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)if(vt[j])
for(pii &x:e[j])
if(vt[x.fi]&&g[i][j]>g[i-1][x.fi]+x.se)g[i][j]=g[i-1][x.fi]+x.se;
}
inline void solve(){
n=read(),m=read(),str=read(),des=read();
for(int i=1;i<=m;i++){
int u=read(),v=read(),w=read();
e[u].pb(pii(v,w)),e[v].pb(pii(u,w));
}
memset(f,127/3,sizeof(f));
memset(g,127/3,sizeof(g));
inf=f[0][0];
k=read();
for(int i=1;i<=k;i++)vt[read()]=1;
// cout<<"OK\n";
calcf(),calcg();
// for(int i=1;i<=n;i++)cout<<f[i][str]<<" "<<g[i][str]<<'\n';
ll res=-1;
for(int i=1;i<=k;i++)if(g[i][str]!=inf&&g[i][str]<=f[i][str]){
bool fg1=0,fg2=0;
for(int j=1;j<i&&!fg2;j++){
if(f[j][str]!=inf)fg1=1;
if(f[j][str]<g[i][str])fg2=1;
}
// cout<<fg1<<" "<<fg2<<'\n';
if(!fg1){res=inf;break;}
if(fg2)continue;
ll l=-1,r=inf;
for(int j=1;j<i;j++)if(f[j][str]!=inf)chemn(r,(f[j][str]-g[i][str])/(i-j));
for(int j=i+1;j<=n;j++)if(f[j][str]!=inf)chemx(l,(g[i][str]-f[j][str])/(j-i));
// cout<<l<<" "<<r<<'\n';
if(l<=r)chemx(res,r);
}
if(res==-1)puts("Impossible");
else if(res==inf)puts("Infinity");
else cout<<res<<'\n';
for(int i=1;i<=n;i++)vt[i]=0,e[i].clear();
}
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
int T=read();
while(T--)solve();
}