zoukankan      html  css  js  c++  java
  • 1018 Public Bike Management (30分)

    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.

    The above figure illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex S is the current number of bikes stored at S. Given that the maximum capacity of each station is 10. To solve the problem at S3​​, we have 2 different shortest paths:

    1. PBMC -> S1​​ -> S3​​. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1​​ and then take 5 bikes to S3​​, so that both stations will be in perfect conditions.

    2. PBMC -> S2​​ -> S3​​. This path requires the same time as path 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains 4 numbers: Cmax​​ (≤), always an even number, is the maximum capacity of each station; N (≤), the total number of stations; Sp​​, the index of the problem station (the stations are numbered from 1 to N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers Ci​​ (,) where each Ci​​ is the current number of bikes at Si​​ respectively. Then M lines follow, each contains 3 numbers: Si​​, Sj​​, and Tij​​ which describe the time Tij​​ taken to move betwen stations Si​​ and Sj​​. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then after one space, output the path in the format: 0. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Sp​​ is adjusted to perfect.

    Note that if such a path is not unique, output the one that requires minimum number of bikes that we must take back to PBMC. The judge's data guarantee that such a path is unique.

    Sample Input:

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

    Sample Output:

    3 0->2->3 0
    有问题的思路:
    只利用dijkstra,求出结点值的和最大的最短路径并输出
    问题代码:
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstdlib>
    #include<vector>
    #include<set>
    #include<cmath>
    #include<cstring>
    #include<queue>
    #include<string.h>
    using namespace std;
    const int maxn=1010;
    typedef long long ll;
    const int inf=0x3fffffff;
    int G[maxn][maxn];
    int dist[maxn];
    int data[maxn];
    int number[maxn];
    bool collected[maxn];
    int path[maxn];
    int C,N,Sp,M;
    int findMin(){
        int mind=inf;
        int mindv=-1;
        for(int i=1;i<=N;i++){
            if(collected[i]==false&&dist[i]<mind){
                mind=dist[i];
                mindv=i;
            }
        }
        return mindv;
    }
    void dijkstra(int P,int Sp){
        number[P]=0;
        int v=-1;
        for(int i=1;i<=N;i++){
            dist[i]=G[P][i];
            if(dist[i]<inf){
                path[i]=P;
                number[i]=number[P]+data[i];
            }else{
                path[i]=-1;
            }
        }
        collected[P]=true;
        while(1){
            v=-1;
            v=findMin();
            if(v==-1){
                return ;
            }
            collected[v]=true;
            for(int i=1;i<=N;i++){
                if(collected[i]==false){
                    if(dist[v]+G[v][i]<dist[i]){
                        dist[i]=dist[v]+G[v][i];
                        path[i]=v;
                        number[i]=number[v]+data[i];
                    }
                    else if(dist[v]+G[v][i]==dist[i]){//若路径相等
                        if(number[v]+data[i]>number[i]){
                            number[i]=number[v]+data[i];
                            path[i]=v;//把i放到新路径上
                        }
                    }
                }
            }
        }
    
    }
    
    int main(){
        int a1,b1,c1;
        fill(path,path+maxn,-1);
        fill(number,number+maxn,0);
        fill(collected,collected+maxn,false);
        fill(dist,dist+maxn,inf);
        fill(G[0],G[0]+maxn*maxn,inf);
        scanf("%d %d %d %d",&C,&N,&Sp,&M);
        data[0]=0;
        for(int i=1;i<=N;i++)
        {
            scanf("%d",&data[i]);
        }
        for(int i=1;i<=M;i++)
        {
            scanf("%d %d %d",&a1,&b1,&c1);
            G[a1][b1]=c1;
            G[b1][a1]=c1;
        }
        dijkstra(0,Sp);
    
        int a[maxn];//存储最短路径上的结点
        int cnt=0;//最短路径上,结点的个数
        int sum=0;//最短路径上结点值的和
        a[0]=Sp;
        sum+=data[Sp];
        int i=0;
        for(i=0;path[a[i]]!=-1;i++){
            a[i+1]=path[a[i]];
            sum+=data[a[i+1]];
            cnt++;
        }
        printf("%d ",abs(sum-cnt*C/2));
        for(int i=cnt;i>=0;i--){
            if(i>0){
                printf("%d->",a[i]);
            }
            else{
                printf("%d ",a[i]);
            }
        }
        if(sum-C*cnt/2<=0){
            printf("%d
    ",0);
        }
        else{
            printf("%d
    ",sum-C*cnt/2);
        }
        return 0;
    }

     对这个问题的初步认识:

    题目要求是求出minNeed和minBack,而我这种方法只能求出minNeed,另一个求不出来。也就是不满足最优解

    正确思路:

    先一遍dijkstra找出所有最短路径,并保存在二维vector里面,然后用dfs遍历每一条路,每遍历一条最短路,更新minNeed和minBack

    AC代码:

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstdlib>
    #include<vector>
    #include<set>
    #include<cmath>
    #include<cstring>
    #include<queue>
    #include<string.h>
    using namespace std;
    const int maxn=1010;
    typedef long long ll;
    const int inf=0x3fffffff;
    int G[maxn][maxn];
    int minNeed=inf,minBack=inf;
    int dist[maxn];
    int data[maxn];
    int number[maxn];
    bool collected[maxn];
    vector<int> pre[maxn],path,temppath;  //pre[maxn] 用来存放每个结点在最短路径上的前一个结点,因为最短路径可能有多个,所以他的前面结点也可能有多个
    int C,N,Sp,M;
    void dijkstra(int v){
        int u;
        dist[v]=0;
        for(int i=0;i<=N;i++){
            int mind=inf;
            int minv=-1;
            for(int j=0;j<=N;j++){
                if(collected[j]==false&&dist[j]<mind){
                    mind=dist[j];
                    minv=j;
                }
            }
            u=minv;
            if(u==-1){
                break;
            }
            collected[u]=true;
            for(int i=0;i<=N;i++){
                if(collected[i]==false&&G[u][i]!=inf){
                    if(dist[u]+G[u][i]<dist[i]){
                        dist[i]=dist[u]+G[u][i];
                        pre[i].clear();//清空i的前驱结点,因为找到了更近的前驱
                        pre[i].push_back(u);//把u放到i的前驱结点集里
                    }
                    else if(dist[u]+G[u][i]==dist[i]){
                        pre[i].push_back(u);//碰到了相等路径,就再给i增减前驱结点
                    }
                }
            }
        }
    }
    void dfs(int v){
        temppath.push_back(v);//记录这条路径上的结点
        if(v==0)//如果一条最短路径走到源点,就更新minNeed和minBack
        {
            int need=0,back=0;
            for(int i=temppath.size()-1;i>=0;i--){//计算这条路上的need和back
                int id=temppath[i];
                if(data[id]>0){
                    back+=data[id];
                }
                else{
                    if(back>(0-data[id])){//若back去掉data[i]大于0,那么就去掉
                        back+=data[id];//注:此时data[i]是负的
                    }
                    else{
                        need+=(0-data[id])-back;
                        back=0;//既然都不够了,那肯定带回来就是0了
                    }
                }
            }
            if(need<minNeed){
                minNeed=need;
                minBack=back;
                path=temppath;//保存这条最短路径
            }
            else if(need==minNeed&&back<minBack){
                minBack=back;
                path=temppath;
            }
    //        temppath.pop_back();
    //        return;
        }
        for(int i=0;i<pre[v].size();i++){//对v每条最短路径进行深搜遍历
            dfs(pre[v][i]);
        }
        temppath.pop_back();//在dfs回退的时候,没回退一步,清除该路径上一个结点
    }
    int main(){
        int a1,b1,c1;
        fill(number,number+maxn,0);
        fill(collected,collected+maxn,false);
        fill(dist,dist+maxn,inf);
        fill(G[0],G[0]+maxn*maxn,inf);
        scanf("%d %d %d %d",&C,&N,&Sp,&M);
        data[0]=0;
        for(int i=1;i<=N;i++)
        {
            scanf("%d",&data[i]);
            data[i]=data[i]-C/2;
        }
        for(int i=1;i<=M;i++)
        {
            scanf("%d %d %d",&a1,&b1,&c1);
            G[a1][b1]=c1;
            G[b1][a1]=c1;
        }
        dijkstra(0);
        dfs(Sp);
        printf("%d ",minNeed);
        for(int i=path.size()-1;i>=0;i--){
            if(i>0){
                printf("%d->",path[i]);
            }
            else{
                printf("%d",path[i]);
            }
        }
        printf(" %d
    ",minBack);
        return 0;
    }
  • 相关阅读:
    《安卓网络编程》之第五篇 WebView的使用
    《安卓网络编程》之第四篇 处理URL地址
    《安卓网络编程》之第三篇 使用Apache接口
    《安卓网络编程》之第二篇 java环境下网络通信的综合应用
    《安卓网络编程》之第一篇 java环境下模拟客户端、服务器端
    星辉odoo教程
    星辉odoo教程
    星辉odoo实施
    星辉信息odoo教程
    星辉科技教程-centos搭建wechaty机器人运行环境
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14333032.html
Copyright © 2011-2022 走看看