Some docks are composed of several edges. In some cases, some docks need to be repaired and cannot be used during this period. If you change the route, you will have to spend a certain amount of time to find the minimum cost within the specified number of days.
Idea: Use spfa for short circuit. The key is dynamic planning. I thought about this rule for a long time and finally found myself thinking complicated. I first thought about how to use spfa to process different segments and then change the rules. In this way, not only segmentation is not good, but also movement rules are not easy to write. Only then can we find that the total number of days is only 100, the enumeration start point and end point are only 10000, and the O (kN) with a spfa is less than 1 QW...
Code:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 110using namespace std;int days,points,change,edges,asks;int cost[MAX][MAX],used[MAX][MAX];bool v[MAX];int head[MAX],total;int next[MAX << 3],aim[MAX << 3],length[MAX << 3];int f[MAX];bool vis[MAX];int dp[MAX];inline void Add(int x,int y,int len);inline void SPFA();int main(){cin >> days >> points >> change >> edges;for(int x,y,z,i = 1;i <= edges; ++i) {scanf("%d%d%d",&x,&y,&z);Add(x,y,z),Add(y,x,z);}cin >> asks;for(int x,y,z,i = 1;i <= asks; ++i) {scanf("%d%d%d",&z,&x,&y);for(int j = x;j <= y; ++j)used[j][z] = true;}for(int i = 1;i <= days; ++i)for(int j = i;j <= days; ++j) {memset(v,false,sizeof(v));for(int k = i;k <= j; ++k)for(int z = 2;z < points; ++z)if(used[k][z])v[z] = true;SPFA();cost[i][j] = f[points] * (f[points] >= 0x3f3f3f3f ? 1:(j - i + 1));}memset(dp,0x3f,sizeof(dp));dp[0] = 0;for(int i = 1;i <= days; ++i)for(int j = 0;j < i; ++j)dp[i] = min(dp[i],dp[j] + cost[j + 1][i] + change);cout << dp[days] - change << endl;return 0;}inline void Add(int x,int y,int len){next[++total] = head[x];aim[total] = y;length[total] = len;head[x] = total;}inline void SPFA(){static queue<int> q;while(!q.empty())q.pop();memset(f,0x3f,sizeof(f));memset(vis,false,sizeof(vis));q.push(1);f[1] = 0;while(!q.empty()) {int x = q.front(); q.pop();vis[x] = false;for(int i = head[x];i;i = next[i]) {if(v[aim[i]])continue;if(f[aim[i]] > f[x] + length[i]) {f[aim[i]] = f[x] + length[i];if(!vis[aim[i]])vis[aim[i]] = true,q.push(aim[i]);}}}}
Bzoj 1003 zjoi 2006 Dynamic Planning of Logistics and Transportation + spfa