[ZJOI2006]物流运输-SPFA-DP

先埋坑,

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int inf = 2e9;
struct node{
  int u;
  int v;
  int w;
  int next;
}edge[10005];
int n,m,k,e;
int cnt;
int head[105];
int dp[105];
int cost[105][105];
bool vis[105];
int a[105][105];
bool flag[105];
int d[105];
int num=0;
void addedge(int x,int y,int w){
  num++;
  edge[num].v=y;
  edge[num].w=w;
  edge[num].next=head[x];
  head[x]=num;
}
int sbfa(){
  memset(d,0x7f,sizeof(d));
  memset(vis,0,sizeof(vis));
  d[1]=0;
  vis[1]=1;
  queue<int>que;
  while(!que.empty()){
    que.pop();
  }
  que.push(1);
  while(!que.empty()){
      int s=que.front();
      que.pop();
      vis[s]=0;
      for (int i=head[s];i;i=edge[i].next){
           // cout<<"--"<<endl;
         if (d[edge[i].v]>d[s]+edge[i].w && !flag[edge[i].v]){
            d[edge[i].v]=d[s]+edge[i].w;
            if (!vis[edge[i].v]){
                vis[edge[i].v]=1;
               que.push(edge[i].v);
            }
         }
      }
   }
   return d[m];
}
int main(){
  int x,y,z;
  while(~scanf("%d%d%d%d",&n,&m,&k,&e)){
     num=0;
     for (int i=1;i<=e;i++){
       scanf("%d%d%d",&x,&y,&z);
       addedge(x,y,z);
       addedge(y,x,z);
     }
     scanf("%d",&cnt);
     int p,tmp1,tmp2;
     for (int i=1;i<=cnt;i++){
        scanf("%d%d%d",&p,&tmp1,&tmp2);
        for (int j=tmp1;j<=tmp2;j++){
            a[p][j]=1;
        }
     }
     for (int i=1;i<=n;i++){
        for (int j=i;j<=n;j++){
           memset(flag,0,sizeof(flag));
           for (int k=1;k<=m;k++)
               for (int l=i;l<=j;l++)
                  flag[k]|=a[k][l];
                  cost[i][j]=sbfa();
        }
     }
     for (int i=1;i<=n;i++)
       for (int j=i;j<=n;j++){
          if (cost[i][j]<inf)cost[i][j]*=(j-i+1);
       }
      memset(dp,0x7f,sizeof(dp));
     for (int i=1;i<=n;i++){
        dp[i]=cost[1][i];
     }

     for (int i=2;i<=n;i++){
        for (int j=1;j<i;j++){
            dp[i]=min(dp[i],dp[j]+cost[j+1][i]+k);
        }
     }
     printf("%d\n",dp[n]);
  }
  return 0;
}

 

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务