• Public Bike Management (30)


    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


    pat上有一组没过,牛客上过了


      1 #include <bits/stdc++.h>
      2 #define N 550
      3 #define inf 0x3f3f3f3f
      4 using namespace std;
      5 struct Node{
      6     int to, value;
      7 };
      8 struct Dis{
      9     int to,value,bring,back;
     10     friend bool operator<(const Dis &a,const Dis &b){
     11         return a.value > b.value;
     12     }
     13 };
     14 stack<int> st;
     15 vector<Node> v[N];
     16 int path[N],val[N],vis[N],fa[N],cost[N];
     17 int m,n,s,t,ans = 0,mini_val = inf,bring_min = 0, back_min = 0;
     18 bool flag;
     19 priority_queue<Dis> q;
     20 void dijst(){
     21     Dis dis;
     22     dis.to  = 0, dis.value = 0, dis.bring = 0, dis.back = 0;
     23     q.push(dis);
     24     while(!q.empty()){
     25         dis = q.top();
     26         q.pop();
     27         if(vis[dis.to] == 1)
     28             continue;
     29         vis[dis.to] = 1;
     30         if(dis.to == s)
     31             break;
     32         val[dis.to] = min(val[dis.to], dis.value);
     33         for(int i = 0; i < v[dis.to].size(); i++){
     34             Dis ans;
     35             ans.to = v[dis.to][i].to;
     36             ans.value = dis.value + v[dis.to][i].value;
     37             if(ans.value <= val[ans.to]){
     38                 val[ans.to] = ans.value;
     39                 path[ans.to] = dis.to;
     40             }
     41             if(cost[ans.to] < 0){
     42                 int add = dis.back + cost[ans.to];
     43                 if(add < 0){
     44                     ans.bring=dis.bring -add;
     45                     ans.back = 0;
     46                 }else{
     47                     ans.bring = dis.bring;
     48                     ans.back  = add;
     49                 }
     50             }else{
     51                 int add = dis.back + cost[ans.to];
     52                 ans.bring = dis.bring;
     53                 ans.back = add;
     54             }
     55             if(ans.to == s){
     56                 if(ans.value < mini_val){
     57                     mini_val = ans.value;
     58                     bring_min = ans.bring, back_min = ans.back;
     59                     for(int i = 0 ; i <= n; i++){
     60                         fa[i] = path[i];
     61                         // cout <<fa[i]<<" ";
     62                     }
     63                 }else if(ans.value == mini_val){
     64                     if(ans.bring < bring_min){
     65                         bring_min = ans.bring, back_min = ans.back;
     66                         for(int i = 0 ; i <= n; i++){
     67                             fa[i] = path[i];
     68                             // cout <<fa[i]<<" ";
     69                         }
     70                     }else if(ans.bring == bring_min){
     71                         if(ans.back < back_min){
     72                             back_min = ans.back;
     73                             for(int i = 0 ; i <= n; i++){
     74                                 fa[i] = path[i];
     75                                 // cout<<fa[i]<<" ";
     76                             }
     77                         }
     78                     }
     79                 }
     80             }
     81             q.push(ans);
     82         }
     83     }
     84 }
     85 
     86 int main(){
     87     cin >> m >> n >> s >> t;
     88     m >>= 1;
     89     for(int i = 1; i <= n; i++){
     90         cin >> cost[i];
     91         cost[i] = cost[i] - m;
     92     }
     93     int x,y,c;
     94     for(int i = 0; i < t; i++){
     95         cin >>x>>y>>c;
     96         v[x].push_back({y,c});
     97         v[y].push_back({x,c}); 
     98     }
     99     memset(path,-1,sizeof(path));
    100     memset(vis,0,sizeof(vis));
    101     memset(val,inf,sizeof(val));
    102     dijst();
    103     while(s != 0){
    104         st.push(s);
    105         s = fa[s];
    106     }
    107     cout << bring_min<<" "<<"0";
    108     while(!st.empty()){
    109         cout <<"->"<<st.top();
    110         st.pop();
    111     }
    112     cout << " "<<back_min<<endl;
    113     return 0;
    114 }
  • 相关阅读:
    Vim快捷键键位图大全
    Docker快速入门
    针对base64编码和URIEncode的一点研究
    JavaEE初学笔记之Servlet与Tomcat
    【编码】彻底弄懂ASCII、Unicode、UTF-8之间的关系
    React的世界观及与Vue之比较
    彻底搞懂CSS文本、空白换行问题
    Vue实现懒加载的基本思路
    CSS中的px与物理像素、逻辑像素、1px边框问题
    Vue插值文本换行问题
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11018691.html
Copyright © 2020-2023  润新知