您的位置:首页 > 其它

PAT (Advanced Level)1018. Public Bike Management (30) 迪杰斯特拉算法 DFS 递归(难)

2018-03-01 15:53 579 查看
题目链接

1018. Public Bike Management (30)

时间限制400 ms
内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
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.


Figure 1Figure 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 tak
4000
en 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 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
源代码参考Source//用dijkstra算法求出并用path记录多条路径,然后dfs筛选最优路径
//注意:send和back的记录;在沿途中,如果count<0的话,说明需要send,

#include <stdio.h>
#include <stdlib.h>
#define MAX 500+1
#define INF 0x7fffff

struct path
{
int size;//直接前驱节点的个数
int vp[MAX];//直接前驱节点
}path[MAX];//储存满足条件的多条最短路径
int stack[MAX], top = 0;//储存路径
int anspath[MAX], x = 0;//储存最终结果路径
int a[MAX][MAX], vertex[MAX];//邻接矩阵和顶点信息
int dist[MAX], visited[MAX];//dist数组和visited数组
int cmax, n, sp, m;//四个参量
int send = INF, back = INF;//send和back数量

void Initpath(void)
{
int i, j;
for (i = 0; i <= n; ++i)
for (j = 0; j <= n; ++j)
path[i].vp[j] = -1;
}
void DFS(int v)//找到最优路径
{
int i;
stack[top++] = v;//每次将顶点v收入stack保存路径
if (v == 0)//标志该次路径结束,比较选择最短路径
{
int s = 0, count = 0;
for (int i = top - 2; i >= 0; --i)//正向遍历
{
count += vertex[stack[i]] - cmax / 2;
if (count<0)//count小于0,需要从源站带车过来,更新s,并把count置为0;
{
s += -count;
count = 0;
}
}
if (s<send || (s == send&&count<back))
{
send = s;
back = count;
x = 0;
for (i = top - 1; i >= 0; --i)
anspath[x++] = stack[i];
}
return;
}
for (i = 0; i<path[v].size; ++i)//对终点v的所有前驱节点进行递归调用
{
DFS(path[v].vp[i]);
--top;//pop栈顶元素
}
}

int main()
{
scanf("%d %d %d %d", &cmax, &n, &sp, &m);
for (int v = 1; v <= n; ++v)//读入顶点信息
scanf("%d", &vertex[v]);
for (int i = 0; i <= n; ++i)//迪杰斯特拉算法开始
{
visited[i] = 0;//初始化visited数组
for (int j = 0; j <= n; ++j)//初始化邻接矩阵
{
if (i != j)
a[i][j] = INF;
else
a[i][j] = 0;
}
}
for (int i = 0; i<m; ++i)//读入边数据
{
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
a[u][v] = a[v][u] = w;
}
Initpath();
for (int i = 0; i <= n; ++i)//初始化dist数组和路径
{
dist[i] = a[0][i];
if (i != 0 && a[0][i]<INF)
path[i].vp[path[i].size++] = 0;
}
visited[0] = 1;
int min, v, w;
for (int i = 1; i <= n; ++i)//dijkstra找到多条路径
{
min = INF;
for (int j = 0; j <= n; ++j)
{
if (visited[j] == 0 && dist[j]<min)
{
min = dist[j];
v = j;
}
}
visited[v] = 1;
for (w = 0; w <= n; ++w)
{
if (visited[w] == 0 && a[v][w]<INF)
{
if (dist[w]>dist[v] + a[v][w])
{
dist[w] = dist[v] + a[v][w];
path[w].vp[0] = v;
path[w].size = 1;
}
else if (dist[w] == dist[v] + a[v][w])
path[w].vp[path[w].size++] = v;//不好理解:如果通过中间点到达目的点的最短路径不止一条,说明他的前驱节点不止一个,需要在总述上加1
}
}
}
DFS(sp);//dfs找到最优路径
printf("%d ", send);
for (int i = 0; i<x - 1; ++i)
printf("%d->", anspath[i]);
printf("%d %d", sp, back);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: