【PAT1018】Public Bike Management

题目描述:
There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.
The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.
When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.

输入测试

10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1

输出结果

3 0->2->3 0

思路分析:
这里写图片描述

#include<iostream>  
#include<fstream>  
#include<vector>  
using namespace std;  
#define MAX 505  
#define INF 10000  

int cap,N,sp,M;  
int vex;  
int dist[MAX][MAX];  
int bike[MAX];  
#define PF cap/2  

vector<int> curpath;  
vector<int> shortpath;  
int minsend=INF,minback=INF;  
int minlen=INF;  
int cursend=0,curback=0;  
int curlen=0;  
bool visit[MAX]={0};  

void dfs(int cur){  
    if(curlen>minlen)   
        return;  
    if(cur==sp){  
    //到达目标点,看是否最优   
        if(curlen<minlen){  
            minlen=curlen;  
            minsend=cursend;  
            minback=curback;  
            shortpath=curpath;  
        }  
        else if(curlen==minlen){  
            if(cursend<minsend||(cursend==minsend&&curback<minback)){  
                minsend=cursend;  
                minback=curback;      
                shortpath=curpath;  
            }     
        }  
        return;  
    }  
    for(int i=1;i<vex;i++){  
        if(visit[i]==true||dist[cur][i]==INF)  
            continue;  
        int lastsend=cursend;  
        int lastback=curback;  
        //计算到达当前点的send和back数   
        if(bike[i]+curback<PF){  
            cursend+=PF-bike[i]-curback;  
            curback=0;  
        }  
        else{  
            curback=bike[i]+curback-PF;  
        }  
        visit[i]=true;  
        curpath.push_back(i);  
        curlen+=dist[cur][i];  
        dfs(i);  
        curpath.pop_back();  
        visit[i]=false;  
        curlen-=dist[cur][i];  
        cursend=lastsend;  
        curback=lastback;  
    }  
}  
int main(){  
    cin>>cap>>N>>sp>>M;  
    //初始化,距离置为INF   
    vex=N+1;  
    for(int i=0;i<vex;i++){  
        for(int j=0;j<vex;j++){  
            dist[i][j]=dist[j][i]=INF;  
        }  
    }  

    for(int i=1;i<vex;i++){  
        cin>>bike[i];  
    }  
    for(int k=0;k<M;k++){  
        int i,j;  
        cin>>i>>j;  
        cin>>dist[i][j];  
        dist[j][i]=dist[i][j];  
    }  

    dfs(0);  

    printf("%d 0",minsend);   
    for(int i=0;i<shortpath.size();i++){  
        printf("->%d",shortpath[i]);  
    }  
    printf(" %d",minback);  
    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值