题解:
题目大意:给定一个无向图,运输n天,其中有些天有些点不能走,更换路线代价为k,求代价总和.
首先令cost[i][j]为第i天到第j天都走同一路线的最小花销,这个用SPFA处理.
然后就是动规的问题了.令f[i]为1~i天的最小花销
则f[i]=min{ f[j]+cost[j+1][i]+k}(0 <= <script id="MathJax-Element-38" type="math/tex"><=</script>j < <script id="MathJax-Element-39" type="math/tex"><</script>i).
表示智商已下线…一定要多刷题了…
AC code:
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll N=101;
const ll INF=1<<28;
ll n,m,k,e,d,tot;
ll head[N],p[N],a[N],b[N],f[N];
ll g[N][N];
struct edge{
ll v,w,next;
edge() {}
edge(ll v,ll w,ll next):v(v),w(w),next(next) {}
}E[N*N];
void addedge(ll u,ll v,ll w){
E[++tot]=edge(v,w,head[u]);
head[u]=tot;
E[++tot]=edge(u,w,head[v]);
head[v]=tot;
}
ll SPFA(ll L,ll R){
bool h[N]={0};
ll dist[N]={0};
queue<ll> Q;
for(ll i=1;i<=d;i++)
if((L<=a[i]&&a[i]<=R)||(L<=b[i]&&b[i]<=R)||(a[i]<=L&&b[i]>=R)) h[p[i]]=1;
for(ll i=2;i<=m;i++) dist[i]=INF;
Q.push(1);
while(!Q.empty()){
ll x=Q.front();
Q.pop();
for(ll i=head[x];i;i=E[i].next){
ll y=E[i].v,w=E[i].w;
if(h[y]||dist[x]+w>=dist[y]) continue;
dist[y]=dist[x]+w;
Q.push(y);
}
}
return dist[m];
}
int main(){
scanf("%lld%lld%lld%lld",&n,&m,&k,&e);
for(ll i=1;i<=e;i++){
ll u,v,w;
scanf("%lld%lld%lld",&u,&v,&w);
addedge(u,v,w);
}
scanf("%lld",&d);
for(ll i=1;i<=d;i++) scanf("%lld%lld%lld",&p[i],&a[i],&b[i]);
for(ll i=1;i<=n;i++)
for(ll j=i;j<=n;j++)
g[i][j]=SPFA(i,j)*(j-i+1);
for(ll i=1;i<=n;i++){
f[i]=g[1][i];
for(ll j=2;j<=i;j++) f[i]=min(f[i],f[j-1]+g[j][i]+k);
}
printf("%lld\n",f[n]);
return 0;
}