1018. Public Bike Management (30)

最短路的变体

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
int Cmax,N,Sp,M;
int C[505];
int Road[505][505];
bool isReached[505];

int curLen=0;
int minLen=1<<30;
int curBike=0;
int minBike=1<<30;
int curSend=0;
int minSend=1<<30;
vector<int> curRoad;
vector<int> minRoad;
void dfs(int cur)
{
  if(curLen>minLen)
    return;
  if(cur == Sp)
  {
    bool choosed = false;
    if(curLen<minLen)
    {
      choosed = true;

    }else if(curLen == minLen)
    {
      if(curSend<minSend)
      {
        choosed = true;
      }else if(curSend == minSend)
      {
        if(curBike<minBike)
        {
          choosed = true;
        }
      }
    }
    if(choosed)
    {
      minLen =  curLen;
      minSend = curSend;
      minBike = curBike;
      minRoad = curRoad;
    }
    return;
  }
  for(int i=0;i<=N;i++)
  {
    if(!isReached[i] && Road[cur][i]>0)
    {
      isReached[i] = true;
      curLen+= Road[cur][i];

      int lastCurSend = curSend;
      int lastCurBike = curBike;
      if(C[i]+curBike<Cmax/2){
        curSend += Cmax/2 - (C[i]+curBike);
        curBike = 0;
      }else{
        curBike = C[i]+curBike-Cmax/2;
      }
      curRoad.push_back(i);
      dfs(i);
      curRoad.pop_back();
      curSend = lastCurSend;
      curBike = lastCurBike;
      curLen-= Road[cur][i];
      isReached[i] = false;
    }
  }
}

int main(){
    scanf("%d%d%d%d",&Cmax,&N,&Sp,&M);
    for(int i=1;i<=N;i++){
        scanf("%d",C+i);
    }
    for(int i=0;i<M;i++){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        Road[a][b]=c;
        Road[b][a]=c;
    }
    isReached[0]=true;
    dfs(0);
    printf("%d ",minSend);
    printf("%d",0);
    for(int i=0;i<minRoad.size();i++){
        printf("->%d",minRoad[i]);
    }
    printf(" %d",minBike);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值