【动态规划】【最短路】[BZOJ 1003]物流运输trans
简直是醉了,这道题本来想了一会儿,然后看了看数据边的数量顶天了才400然后时间最多才100那么直接用SPFA 复杂度
#include <cstdio>
#include <algorithm>
#include <queue>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int MAXE = 20000;
const int MAXN = 200;
const int MAXM = 40;
const int INF = 300000000;
struct node{
int v, w;
node *next;
}Edges[MAXE*2], *ecnt=Edges, *adj[MAXM+10];
bool life[MAXN+10];
int f[MAXN+2], n, m, e, k, now, to, d;
pair<int, pair<int,int> > ord[310];
void addedge(int u, int v, int w){
++ecnt;
ecnt->v = v;
ecnt->w = w;
ecnt->next = adj[u];
adj[u] = ecnt;
}
queue<int> que; int dis[MAXM+10];
inline int SPFA(int st, int ed){
for(int i=1;i<=m;i++)
dis[i] = INF;
memset(life, 0, sizeof life);
for(int i=0;i<d;i++){
if(ord[i].second.first > ed || ord[i].second.second < st) continue;
life[ord[i].first] = true;
}
while(!que.empty()) que.pop();
que.push(1);dis[1] = 0;
while(!que.empty()){
now = que.front(); que.pop();
for(node *p=adj[now];p;p=p->next){
to = p->v;
if(!life[to] && dis[to] > dis[now] + p->w){
dis[to] = dis[now] + p->w;
que.push(to);
}
}
}
return dis[m];
}
int main(){
int tmp;
scanf("%d%d%d%d", &n, &m, &k, &e);
for(int i=0;i<e;i++){
scanf("%d%d%d", &now, &to, &tmp);
addedge(now, to, tmp);
addedge(to, now, tmp);
}
scanf("%d", &d);
for(int i=0;i<d;i++){
scanf("%d%d%d", &tmp, &now, &to);
ord[i] = (make_pair(tmp, make_pair(now, to)));
}
for(int i=1;i<=n;i++){f[i] = INF;}
f[0] = -k;
for(int i=1;i<=n;i++)
for(int j=0;j<i;j++){
if(f[j] >= INF || (tmp = SPFA(j+1, i)) >= INF) continue;
f[i] = min(f[i], f[j]+tmp*(i-j)+k);
}
printf("%d\n", f[n]);
return 0;
}