c 编写Public Bike Management
来源:互联网 发布:package java 编辑:程序博客网 时间:2024/06/10 15:12
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:
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.
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 Spis 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 56 7 00 1 10 2 10 3 31 3 12 3 1
Sample Output:
3 0->2->3 0
一开始想利用贪心算法让车数量与时间同时最优,测试时发现在某些情况下车数量并不能最优,
如:10 5 4 5 6 7 5 00 1 10 2 11 3 12 3 13 4 1
于是考虑先记录所有最短路径,然后再找出车数量最优的路径
代码如下:
#include <stdio.h>#include <math.h>#define INIFINITY 10000000struct {int parent;//record parentint Col;//record different waysint send;//record num of sending bikesint back;//record num of back bikes} path[1000][1000];//record all the shortest pathint C, N, sp, M, G[1000][1000], cur[1000];int time[1000], known[1000], count[1000];void findpath(int x);//find the shortest waysint main(void){int v, w, i, j, k, l; int way[1000];//record the aim wayint send, back, pre, prep;scanf("%d%d%d%d", &C, &N, &sp, &M);//get the Capacity of each station,the number of stations,the problem station,the number of edgefor (i = 1; i <= N; i++) {scanf("%d", &cur[i]);//get the number of bikes of each stationtime[i] = -1; known[i] = 0;count[i] = 0;//initialnize}//initialnizefor (i = 1; i <= N; i++){for (j = 1; j <= N; j++) {G[i][j] = 0;path[i][j].parent = path[i][j].Col = 0;path[i][j].send = path[i][j].back = 0;}}for (i = 0; i < M; i++) {scanf("%d %d %d", &v, &w, &j);G[w][v] = G[v][w] = j;}//record the edgesfindpath(sp);//find the shortest path//find the best waysend = back = INIFINITY;for (i = 0; i < count[sp]; i++) {if (send > path[sp][i].send ||(send == path[sp][i].send && back > path[sp][i].back)) {send = path[sp][i].send;back = path[sp][i].back;pre = path[sp][i].parent;prep = path[sp][i].Col;}}for (i = pre, j = prep, k = 0; i >= 0; ) {way[k++] = i;pre = path[i][j].parent;prep = path[i][j].Col;i = pre;j = prep;} //print the ways and the send bikes number and the back bikes numberprintf("%d ", send);for (--k; k >= 0; k--)printf("%d->", way[k]);printf("%d %d\n", sp, back);return 0;}void findpath(int x){int v, w, i, j, k, len, temp;//initialnize the starttime[0] = 0;path[0][0].parent = -1;path[0][0].Col = -1;path[0][0].send = path[0][0].back = 0;count[0] = 1;//find the index of minimum timewhile (!known[x]) {len = INIFINITY;for (i = 0; i <= N; i++) {if (known[i])continue;if (time[i] >= 0 && time[i] < len) {len = time[i];v = i;}}//mark it as knownknown[v] = 1;//find time waving waysfor (w = 1; w <= N; w++) {if (!G[v][w] || known[w])//if two station are not connected or the station already know ,do nothingcontinue;if (time[w] >= 0 && time[w] < time[v] + G[v][w])//if time>0 and time is shorter ,do nothing continue;else if (time[w] == -1 || time[w] >= time[v] + G[v][w]) {if (time[w] == -1 || time[w] > time[v] + G[v][w]) //if time==-1(means this is the first way),or time is longer ,setcount[w]==0count[w] = 0;time[w] = time[v] + G[v][w];//update the time temp = fabs(cur[w] - C/2);for (i = 0, j = count[w]; i < count[v]; i++, j++) {path[w][j].parent = v;path[w][j].Col = i;//record the path (parent if path[parent][Col])//caculate the needing changes of bike bumberif (cur[w] >= C/2) {path[w][j].back = path[v][i].back + temp;path[w][j].send = path[v][i].send;}else {if (temp <= path[v][i].back) {path[w][j].back = path[v][i].back - temp;path[w][j].send = path[v][i].send;}else {path[w][j].send = path[v][i].send + temp - path[v][i].back;path[w][j].back = 0;}}}count[w] = j;//record the kinds of ways}}}}
- c 编写Public Bike Management
- 1018. Public Bike Management
- PAT1018. Public Bike Management
- 1018.Public Bike Management
- PAT1018 Public Bike Management
- 1001 Public Bike Management
- Public Bike Management
- PAT1018. Public Bike Management
- PAT_1018. Public Bike Management
- 【PAT1018】Public Bike Management
- 1018. Public Bike Management (30)
- 1018. Public Bike Management (30)
- PAT 1018 Public Bike Management
- 1018. Public Bike Management (30)*
- 1018. Public Bike Management (30)
- 1018. Public Bike Management (30)
- 1018. Public Bike Management (30)
- 1018. Public Bike Management (30)
- triplet loss 原理以及梯度推导
- Openwrt学习笔记(二)——Flash Layout and file system
- 1112. Stucked Keyboard (20)
- thinkphp教学系列
- 记录一下glide与Imageloader的区别。
- c 编写Public Bike Management
- oracle控制台密码修改
- c编写 Shortest Path [4]
- json数组直接转换成对象
- alibaba/druid 常见问题
- 【jzoj3630】【汕头市选2014】【分叉】【树形动态规划】
- c编写 Shortest Path [3]
- 通信算法之十六:循环卷积与线性卷积在通信系统中物理层基带中的应用
- Zeppelin使用心得