Bzoj 1003 zjoi 2006 Dynamic Planning of Logistics and Transportation + spfa

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.