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.
![1018. Public Bike Management (30) - 嫒mei贝 - ⊙-→棒棒糖ing](http://www.patest.cn/upload/11_lrrk0m5o4kg.jpg)
Figure 1
Figure 1 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 1Sample Output:
3 0->2->3 0
2015-7-19在阅读了new/delete;new[]/delete[]最好配对使用,我就该了尾巴重新弄了一下。但是对这个问题还是有疑问的。我以前弄的不符合这个,所以怎么弄有点模糊
下面是我以前没有根据以上话弄的。那以后如果学得多了,真正知道甚么问题,再把下面的改了
NumberOff *mouse;
mouse=new NumberOff[n];
while(add--)
{
walk_right(&mouse[n-1],&mouse[add]);
}
delete[] mouse;
======================================
int runtime(MinTime**MT,int n,int m){}
MinTime**MT;
MT=new MinTime*[n];
for(add=0;add<n;add++)
MT[add]=new MinTime [m];
n=runtime(MT,n,m);
for(add=0;add<n;add++)
delete[] MT[add];
delete []MT;
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
7月19日 21:25 | 答案正确 | 30 | 1018 | C++ (g++ 4.7.2) | 2 | 436 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 384 | 12/12 |
1 | 答案正确 | 1 | 304 | 2/2 |
2 | 答案正确 | 1 | 308 | 2/2 |
3 | 答案正确 | 1 | 256 | 2/2 |
4 | 答案正确 | 1 | 308 | 2/2 |
5 | 答案正确 | 1 | 308 | 2/2 |
6 | 答案正确 | 1 | 308 | 2/2 |
7 | 答案正确 | 1 | 300 | 3/3 |
8 | 答案正确 | 1 | 300 | 2/2 |
9 | 答案正确 | 2 | 436 | 1/1 |
#include<iostream> using namespace std; #define MAX 0x7fffffff struct Spnum { int bikes; long int LastIndex; }; struct Sroad { int Node; long int Costtime; long int FrontIndex; }; long int RSGmap(Spnum *Spi, Sroad *Sr,long int temp,int Si,int Sj,long int Tij) { Sr[temp].Node = Sj; Sr[temp].Costtime = Tij; Sr[temp].FrontIndex = Spi[Si].LastIndex; Spi[Si].LastIndex = temp++; Sr[temp].Node = Si; Sr[temp].Costtime = Tij; Sr[temp].FrontIndex = Spi[Sj].LastIndex; Spi[Sj].LastIndex = temp++; return temp; } void Starset(int N,Spnum *Spi,Sroad *Sr,long int M) { int Si, Sj,i; Spi[0].bikes=0; Spi[0].LastIndex = 0; long int temp,j,Tij; for (i = 0; i < N;) { i++; cin >> Spi[i].bikes; Spi[i].LastIndex = 0; } for (j = 0,temp=1; j < M; j++ ) { cin >> Si >> Sj >> Tij; temp = RSGmap(Spi, Sr,temp,Si,Sj,Tij); } } void ReturnRoad(int* Outroads , int*Outroad, int i) { for (; i >0; i--) { Outroad[i]=Outroads[i]; } } void EScompare(Spnum *Spi, int *Outroads, int*Outroad, long int Cmax, long int*Takemin, long int* Tbackmin, int i, long int Btempmin, int*ireturn, bool*Gameover) { long int Take=MAX, Back=MAX,j; Btempmin = 0; for (j = 1; j <=i; j++) { Btempmin += Spi[Outroads[j]].bikes-Cmax/2; if (Take>Btempmin)Take = Btempmin; } Take = Take > 0 ? 0 : -Take; Back = Take+Btempmin; if (Take == (*Takemin)&&Back < (*Tbackmin)||Take<(*Takemin)) { (*Takemin) = Take; (*Tbackmin) = Back; (*ireturn) = i; ReturnRoad(Outroads, Outroad, i); if (0 == (*Takemin) && 0 == (*Tbackmin)) (*Gameover) = false; } } void RSDFS(Spnum *Spi, Sroad *Sr, int* Outroads , int*Outroad, bool *flag, int now, int goal, long int Cmax, long int Timemin,long int Tmin , long int*Takemin, long int Bmin, long int* Tbackmin, int i, int*ireturn,bool*Gameover) { long int index,Ttempmin,Btempmin; int id ; index = Spi[now].LastIndex; flag[now] = false; while (index > 0&&(*Gameover)) { id = Sr[index].Node; Btempmin = Bmin + Spi[id].bikes - Cmax / 2; Ttempmin = Tmin + Sr[index].Costtime; Outroads[i] = id; if (flag[id]&&id != goal&&Ttempmin<Timemin) { RSDFS(Spi, Sr, Outroads, Outroad, flag, id, goal, Cmax, Timemin, Ttempmin, Takemin, Btempmin, Tbackmin, i + 1, ireturn,Gameover); } else if (id == goal&&Ttempmin == Timemin) { EScompare(Spi, Outroads, Outroad, Cmax, Takemin, Tbackmin, i, Btempmin, ireturn, Gameover); } index = Sr[index].FrontIndex; } flag[now] = true; } void TRSDFS(Spnum *Spi, Sroad *Sr, bool *flag, int now, int goal, long int *Timemin, long int Tmin, long int *Takemin ) { long int index, Ttempmin ; int id; index = Spi[now].LastIndex; flag[now] = false; while (index > 0 ) { id = Sr[index].Node; Ttempmin = Tmin + Sr[index].Costtime; if (flag[id]&&Ttempmin < (*Timemin)&&id != goal) { TRSDFS(Spi, Sr, flag, id, goal, Timemin, Ttempmin, Takemin ); } else if (id == goal&&Ttempmin <= (*Timemin) ) { (*Timemin) = Ttempmin; } index = Sr[index].FrontIndex; } flag[now] = true; } void Flag_star(bool*flag,int n) { while (n)flag [n--]= true; flag[n] = true; } int main() { int Cmax, N,Sp ,ireturn; long int M,Takemin,Timemin,Tbackmin; int *Outroads ; int *Outroad; bool *flag; bool Gameover; Sroad *Sr; Spnum *Spi; cin >> Cmax >> N >> Sp >> M; flag = new bool[N + 1]; Spi = new Spnum[N + 1]; Outroads = new int[N+1]{0}; Outroad = new int[N+1]{0}; Sr = new Sroad[2 * (M + 2)]; Sr[0].Costtime = 0; Sr[0].FrontIndex = 0; Sr[0].Node = 0; Starset(N,Spi,Sr,M); Takemin=Tbackmin=Timemin = MAX; Flag_star(flag, N); TRSDFS(Spi, Sr, flag, 0, Sp, &Timemin, 0, &Takemin ); ireturn = 0; Flag_star(flag, N); Gameover = true; RSDFS(Spi, Sr, Outroads, Outroad, flag, 0, Sp, Cmax, Timemin,0,&Takemin,0,&Tbackmin,1,&ireturn, &Gameover); cout <<Takemin << " 0"; for (Sp = 1; Sp<=ireturn ; Sp++) { cout <<"->" <<Outroad[Sp]; } cout << " "<<Tbackmin <<endl; delete []flag; delete []Spi; delete []Outroads ; delete []Outroad; delete []Sr; system("pause");/*这里是在vs2013在测试所以就停顿看结果*/ return 0; }