首页 > 其他分享 >1018 Public Bike Management

1018 Public Bike Management

时间:2023-06-06 16:13:21浏览次数:45  
标签:tmp Management int sum back Bike 1018 path cmax

题目:

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​(≤100), always an even number, is the maximum capacity of each station; N (≤500), 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​ (i=1,⋯,N) 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−>S1​−>⋯−>Sp​. 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

 

题目大意:每个自行车车站的最大容量为一个偶数cmax,如果一个车站里面自行车的数量恰好为cmax / 2,那么称处于完美状态。如果一个车站容量是满的或者空的,控制中心(处于结点0处)就会携带或者从路上收集一定数量的自行车前往该车站,一路上会让所有的车站沿途都达到完美。现在给出cmax,车站的数量n,问题车站sp,m条边,还有距离,求最短路径,同时需要分别求出从开始携带和最后收集带回控制中心的自行车数量。

如果最短路径有多个,求能带的最少的自行车数目的那条。如果还是有很多条不同的路,那么就找一个从车站带回的自行车数目最少的(带回的时候是不调整的)

分析:开始我的思路是

sum 表示管理中心和路上收集的自行车总数

路中如果遇到缺的站,从总数中补,多的站则收集加入总数sum

最后到终点进行补或收集后,根据sum看还多出或者缺少几辆自行车,优先选择缺少数量最少的,接着是多出数量最少的。

但是这种思想有误,有可能既需要开始携带有需要最后回收。

如果只有Dijkstra是不可以的,因为minNeed和minBack在路径上的传递不满足最优子结构,不是简单的相加的过程,只有在所有可能的最短路径都确定了之后,才能从可能的最短路径中判断并选择最小的need和最小的back。这里选择最小need和最小back的过程就需要使用DFS来解决。

最优子结构就是比如已知从起点A到结点E的最短路径为A->C->B->E, 那么A->C->B一定就是从起点A到节点B的最短路径。

代码:(测试点5,7出错)

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int inf = 99999999;
int cmax, n, sp, m;
int c[505], e[505][505], pre[505], d[505], sum[505] = {0};
bool visited[505] = {0};
int main(){
    scanf("%d%d%d%d", &cmax, &n, &sp, &m);
    for(int i = 1; i <= n; i++){
        scanf("%d", &c[i]);
    }
    fill(e[0], e[0] + 505 * 505, inf);
    fill(d, d + 505, inf);
    for(int i = 0; i < m; i++){
        int start, end, time;
        scanf("%d%d%d", &start, &end, &time);
        e[start][end] = e[end][start] = time;
    }
    for(int i = 0; i <=n; i++){
        e[i][i] = 0;
    }
    pre[0] = -1;
    d[0] = 0;
    for(int i = 0; i <= n; i++){
        int u = -1, minn = inf;
        for(int j = 0; j <= n; j++){
            if(visited[j] == false && d[j] < minn){
                u = j;
                minn = d[j];
            }
        }
        if(u == -1) break;
        visited[u] = true;
        for(int j = 0; j <= n; j++){
            if(visited[j] == false){
                if(d[u] + e[u][j] < d[j]){
                    d[j] = d[u] + e[u][j];
                    if(c[j] > cmax / 2) sum[j] = sum[u] + c[j] - cmax / 2;
                    if(c[j] < cmax / 2) sum[j] = sum[u] - (cmax / 2 - c[j]);
                    pre[j] = u;
                }else if(d[u] + e[u][j] == d[j]){
                    int tmp;
                    if(c[j] > cmax / 2) tmp = sum[u] + c[j] - cmax / 2;
                    if(c[j] < cmax / 2) tmp = sum[u] - (cmax / 2 - c[j]);
                    if(tmp > 0){
                        if(sum[j] < 0){
                            sum[j] = tmp;
                            pre[j] = u;
                        }else if(tmp < sum[j]){
                            sum[j] = tmp;
                            pre[j] = u;
                        }
                    }else{
                        if(sum[j] < 0 && tmp > sum[j]){
                            sum[j] = tmp;
                            pre[j] = u;
                        }
                    }
                }
            }
        }
    }
    printf("%d ", -sum[sp] > 0 ? -sum[sp] : 0);
    int tmp = sp, path[505], index = 0;
    while(tmp != -1){
        path[index++] = tmp;
        tmp = pre[tmp];
    }
    for(int i = index - 1; i >= 0; i--){
        printf("%d", path[i]);
        if(i != 0) printf("->");
    }
    printf(" %d", sum[sp] > 0 ? sum[sp] : 0);
    return 0;
}

 

测试5、7错误原因:(通过一下样例理解)

输入:

10 4 4 5
4 8 9 0
0 1 1
1 2 1
1 3 2
2 3 1
3 4 1

输出:

1 0->1->2->3->4 2

 

满分代码:(Dijkstra + DFS【柳婼】)

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int inf = 99999999;
int cmax, n, sp, m;
int minNeed = inf, minBack = inf;
int e[510][510], dis[510], weight[510];
bool visit[510];
vector<int> pre[510], path, temppath;
void dfs(int v) {
    temppath.push_back(v);
    if(v == 0) {
        int need = 0, back = 0;
        for(int i = temppath.size() - 1; i >= 0; i--) {
            int id = temppath[i];
            if(weight[id] > 0) {
                back += weight[id];
            } else {
                if(back > (0 - weight[id])) {
                    back += weight[id];
                } else {
                    need += ((0 - weight[id]) - back);
                    back = 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++)
        dfs(pre[v][i]);
    temppath.pop_back();
}
int main() {
    fill(e[0], e[0] + 510 * 510, inf);
    fill(dis, dis + 510, inf);
    scanf("%d%d%d%d", &cmax, &n, &sp, &m);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &weight[i]);
        weight[i] = weight[i] - cmax / 2;
    }
    for(int i = 0; i < m; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        scanf("%d", &e[a][b]);
        e[b][a] = e[a][b];
    }
    dis[0] = 0;
    for(int i = 0; i <= n; i++) {
        int u = -1, minn = inf;
        for(int j = 0; j <= n; j++) {
            if(visit[j] == false && dis[j] < minn) {
                u = j;
                minn = dis[j];
            }
        }
        if(u == -1) break;
        visit[u] = true;
        for(int v = 0; v <= n; v++) {
            if(visit[v] == false && e[u][v] != inf) {
                if(dis[v] > dis[u] + e[u][v]) {
                    dis[v] = dis[u] + e[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                }else if(dis[v] == dis[u] + e[u][v]) {
                    pre[v].push_back(u);
                }
            }
        }
    }
    dfs(sp);
    printf("%d 0", minNeed);
    for(int i = path.size() - 2; i >= 0; i--)
        printf("->%d", path[i]);
    printf(" %d", minBack);
    return 0;
}

 

标签:tmp,Management,int,sum,back,Bike,1018,path,cmax
From: https://www.cnblogs.com/yccy/p/17460841.html

相关文章

  • wordpress插件:用WP Media Category Management管理媒体库分类
    一,安装插件:搜索WPMediaCategoryManagement点击立即安装 安装完成后,点击启用点击启用后页面会报错,忽略它返回前一个页面,点这里:提示要自动更新,跳过,也可选允许并继续按默认设置,点SaveSettings二,应用插件:1,添加分类2,修改图片所属分类3,从媒体库选择时:......
  • NIST SP 800-37 Risk Management Framework for Information Systems and Organizatio
    NISTSP800-37RiskManagementFrameworkforInformationSystemsandOrganizationsASystemLifeCycleApproachforSecurityandPrivacy Itstructuredinto3levelorganizationview,businessmissionandinformationsystemview.800-37isshortforNIST......
  • Get-MMagent 是一个命令,通常用于查询与 Microsoft Management Agent (MMAgent) 相关的
    Get-MMagent是一个命令,通常用于查询与MicrosoftManagementAgent(MMAgent)相关的属性和配置信息。MMAgent是一款基于云计算技术的软件代理程序,用于帮助配置管理、安全性和监视方案。在Windows平台上,MMAgent通常用于实现高效的云端管理和自动化操作,包括AzureMonitor等相......
  • poj1018(1)
    其实这道题我也没有完全的弄明白,糊里糊涂就ac了 大致题意:某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1、m2、m3、...、mn个厂家提供生产,而每个厂家生产的同种设备都会存在两个方面的差别:带宽bandwidths和价格prices。现在每种设备都各需要1个,考虑......
  • spring-transaction源码分析(2)EnableTransactionManagement注解
    概述(Javadoc)该注解开启spring的注解驱动事务管理功能,通常标注在@Configuration类上面用于开启命令式事务管理或响应式事务管理。@Configuration@EnableTransactionManagementpublicclassAppConfig{@BeanpublicFooRepositoryfooRepository(){//c......
  • SpringSecurity过滤器之SessionManagementFilter
    SessionManagementFilter检测用户自请求开始以来是否已通过身份验证,如果已通过,则调用SessionAuthenticationStrategy以执行任何与会话相关的活动,例如激活会话固定保护机制或检查多个并发登录。配置如下:@ConfigurationpublicclassMySecurityConfigextendsWebSecurityConfigur......
  • 230423 BMS Safety and Fault Management for Lithium Ion Batteries
    WelcometotheStoffelSystemsInsightsvideoseries.I'mEricStoffel,presidentofStoffelSystems.Today'stopicisBMSsafetyandfaultmanagement.Aswediscussedinapreviousvideo,oneoftheprimaryrolesofaBMSinalithium-ionbat......
  • SQL Server2022以及SQL Server Management Studio(SSMS)的下载和安装
    1.下载安装包:浏览搜索SQLSERVER2022 2.进入页面后,点击下载 3.页面下拉,选择安装windows版,点击选择安装设置 4.选择在window上安装 5.填写自己信息:姓名手机号邮箱等;(这里可以随便填) 6.点击Downloadnow,等待下载完成 7.下载之后打开下载文件,选择下载介质 8.......
  • 论文解析 -- A Survey of AIOps Methods for Failure Management
    此篇Survey是ASystematicMappingStudyinAIOps的后续研究对于AIOPS中占比较高的FailureManagement进行进一步的研究   Comparedtotraditionalapproaches,AIOpsis:•fast,becauseitreactsindependentlyandautomaticallytoreal-timeproblems,with......
  • UVa 10182 Bee Maja (规律&O(1)算法)
    10182-BeeMajaTimelimit:3.000seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123Majaisabee.Shelivesinabeehivewiththousandsofotherbees.Thisbeehivecon......