Project 2

Public Bike Management

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 inperfect 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 vertexSSS is the current number of bikes stored at SSS. Given that the maximum capacity of each station is 10. To solve the problem at S3S_3S3, we have 2 different shortest paths:

  1. PBMC -> S1S_1S1 -> S3S_3S3. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike fromS1S_1S1 and then take 5 bikes to S3S_3S3, so that both stations will be in perfect conditions.

  2. PBMC -> S2S_2S2 -> S3S_3S3. 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:CmaxC_{max}Cmax (≤100\le 100100), always an even number, is the maximum capacity of each station; NNN (≤500\le 500500), the total number of stations; SpS_pSp, the index of the problem station (the stations are numbered from 1 to NNN, and PBMC is represented by the vertex 0); and MMM, the number of roads. The second line contains NNN non-negative numbers CiC_iCi (i=1,⋯,Ni=1,\cdots ,Ni=1,,N) where each CiC_iCi is the current number of bikes at SiS_iSi respectively. Then MMM lines follow, each contains 3 numbers: SiS_iSi,SjS_jSj, and TijT_{ij}Tij which describe the time TijT_{ij}Tij taken to move betwen stations SiS_iSi and SjS_jSj. 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−>⋯−>Sp0->S_1->\cdots ->S_p0>S1>>Sp. Finally after another space, output the number of bikes that we must take back to PBMC after the condition ofSpS_pSp 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

#include<stdio.h>
#include<stdlib.h>

#define MaxVertexNum 505
#define INFINITY 100000

struct GNode{
	int Cmax;
	int N;   /*站点总数*/ 
	int M;   /*边总数*/
	int G[MaxVertexNum][MaxVertexNum];  /*记录每条边的权重*/ 
};

struct Known{
	int top;  /*记录路径时压栈储存*/ 
	int number;  /*记录有几条相同的路径*/ 
	int now;   /*记录当前遍历的节点*/ 
};

struct Result{
	int minsend; /*记录最小send*/ 
	int minback; /*记录最小back*/ 
	int minnumber; /*记录满足答案要求的路径*/ 
};

struct Known *DFS(int Sp, struct Known *know, struct GNode *Graph, int path[][MaxVertexNum], int prepath[], int dist[], int visit[]);
void Dijkstra(int start, int end, int dist[], struct GNode *Graph, int visit[]); /*Dijkstra算法求最短路径*/ 
void initial(struct GNode *Graph, int dist[], int visit[]);  /*初始化函数*/ 
struct Result *compare(int path[][MaxVertexNum], int Sp, struct GNode *Graph, struct Known *konw, struct Result *result, int bikes[]);

int main(void)
{
	int Sp, x, y, weight, i;
	struct GNode *Graph;  /*申明图*/ 
	int bikes[MaxVertexNum];  /*记录每个点储存的自行车数量*/ 
	Graph=(struct GNode*)malloc(sizeof(struct GNode)); /*申请内存空间*/ 
	scanf("%d %d %d %d",&Graph->Cmax,&Graph->N,&Sp,&Graph->M);  
	int dist[Graph->N+1];  /*记录各点到源点的最短路径*/ 
	int visit[Graph->N+1];  /*记录各点是否已经遍历*/ 
	initial(Graph, dist, visit); /*初始化*/ 
	for(i=1; i<=Graph->N; i++)  /*输入每个站点自行车数量*/ 
	{
		scanf("%d",&bikes[i]);
	}
	for(i=0; i<Graph->M; i++)
	{
		scanf("%d %d %d",&x,&y,&weight);
		Graph->G[x][y]=weight; /*无权图双向输入*/ 
		Graph->G[y][x]=weight;
	}
	dist[0]=0;
	visit[0]=1;
	Dijkstra(0,Sp,dist,Graph,visit); /*找到各点到源点的最短路径*/ 
	for(i=1; i<=Graph->N; i++)   /*再一次初始化visit*/ 
	{
		visit[i]=0;
	}
	int path[MaxVertexNum][MaxVertexNum]={0};  /*记录最短路径*/ 
	int prepath[MaxVertexNum]={0}; /*记录当前遍历的路径*/ 
	struct Known *know;  /*储存当前top,now,number数据,便于递归传递*/ 
	know=(struct Known*)malloc(sizeof(struct Known)); /*申请内存*/ 
	know->now=know->number=know->top=0;  /*初始化*/ 
	know=DFS(Sp, know, Graph, path, prepath, dist, visit); /*通过深度便利找到最小距离情况下的路径*/ 
	struct Result *result;
	result=(struct Result*)malloc(sizeof(struct Result));
	result=compare(path, Sp, Graph, know, result, bikes); /*比较各条路径找到正确答案*/ 
	printf("%d ",result->minsend); /*输出最小send*/ 
	for(i=0; i<MaxVertexNum; i++) /*输出最短路径*/ 
	{
		if(path[result->minnumber][i]==Sp)
		{
			printf("%d",path[result->minnumber][i]);
			break;
		}
		else
		{
			printf("%d->",path[result->minnumber][i]);
		}
	}
	printf(" %d\n",result->minback); /*输出最小back*/ 
}

void initial(struct GNode *Graph, int dist[], int visit[]) /*初始化*/ 
{
	int i,j;
	for(i=0; i<=Graph->N; i++)
	{
		for(j=0; j<=Graph->N; j++)
		Graph->G[i][j]=INFINITY;
		dist[i]=INFINITY;
		visit[i]=0;
	}
}

void Dijkstra(int start, int end, int dist[], struct GNode *Graph, int visit[]) /*运用Dijkstra算法计算出最短路径*/ 
{
	int min, v, i, j;
	for(i=1; i<=Graph->N; i++)
	{
		dist[i]=Graph->G[start][i];
	}
	for(i=0; i<Graph->N; i++)
	{
		min=INFINITY;
		for(j=1; j<=Graph->N; j++)
		{
			if(visit[j]==0&&dist[j]<min)  /*找到未遍历的各点距离源点的最近距离*/ 
			{
				v=j;
				min=dist[j];
			}
		}
		if(min==INFINITY) /*如果图只有一个节点则退出*/ 
		break;
		visit[v]=1; /*标记为已经遍历*/ 
		for(j=1; j<=Graph->N; j++)
		{
			if(dist[v]+Graph->G[v][j]<dist[j])  /*如果找到更短的一条路径*/ 
			{
				dist[j]=dist[v]+Graph->G[v][j]; /*更新各点到源点的最短距离*/ 
			}
		}
	}
}
/*深度优先算法*/ 
struct Known *DFS(int Sp, struct Known *know, struct GNode *Graph, int path[][MaxVertexNum], int prepath[], int dist[], int visit[])
{
	int temp;
	int i,j;
	prepath[know->top++]=know->now; /*记录当前遍历的路径*/ 
	if(know->now==Sp) /*如果到达最后的一个节点*/ 
	{
		for(i=0; i<know->top; i++)
		{
			path[know->number][i]=prepath[i];  /*记录当前路径*/ 
		}
		know->number++; /*最短路径数+1*/ 
		know->top--;    /*因为pop出栈顶那个点*/ 
		return know; /*返回top, number, now数据*/ 
	}
	else
	{
		for(j=1; j<=Graph->N; j++)
		{
			if(dist[know->now]+Graph->G[know->now][j]==dist[j]) /*如果路径满足最短路径*/ 
			{
				temp=know->now;
				know->now=j; /*更新当前节点*/ 
				visit[j]=1; /*标记为已经遍历*/ 
				know=DFS(Sp, know, Graph, path, prepath, dist, visit); /*递归,进行深度遍历*/ 
				visit[j]=0; /*退回原路*/ 
				know->now=temp; 
			}
		}
		know->top--; /*因为回退pop出栈顶的点*/ 
		return know; /*返回top, number, now数据*/ 
	}
}
/*比较各条最短路径找到正确答案*/ 
struct Result *compare(int path[][MaxVertexNum], int Sp, struct GNode *Graph, struct Known *know, struct Result *result, int bikes[])
{
	int extra, send, back; /*send表示从源点送出去的数据, extra表示经过一个节点后多余的车辆, back表示最后送回原点的车辆*/  
	result->minsend=result->minback=INFINITY;/*初始化*/ 
	int i,j;
	for(i=0; i<know->number; i++)
	{
		extra=send=back=0;/*初始化*/ 
		for(j=1; j<MaxVertexNum; j++)
		{
			if(path[i][j]==Sp) /*如果到最后一点*/ 
			break;
			else
			{
				if(bikes[path[i][j]]<Graph->Cmax/2) /*如果自行车数小于最大容量的一半*/ 
				{
					if(extra>=Graph->Cmax/2-bikes[path[i][j]]) /*如果extra有多就用extra弥补*/ 
					{
						extra=extra-(Graph->Cmax/2-bikes[path[i][j]]);
					}
					else
					{
						send=send+(Graph->Cmax/2-bikes[path[i][j]]-extra); /*如果extra不够弥补,多余的就用send弥补*/ 
						extra=0; /*extra全部弥补完了,所以清零*/ 
					}
				}
				else if(bikes[path[i][j]]>Graph->Cmax/2) /*如果自行车数大于最大容量的一半*/ 
				{
					extra=extra+bikes[path[i][j]]-Graph->Cmax/2; /*把多余的车数量记在extra上*/ 
				}
			}
		}
		if(bikes[Sp]>=Graph->Cmax/2) /*如果最后一个点大于最大值的一半*/ 
		{
			back=extra+bikes[Sp]-Graph->Cmax/2; /*额外值加上最后一点多余的值就是back*/ 
		}
		else if(bikes[Sp]<Graph->Cmax/2)
		{
			if(extra>=Graph->Cmax/2-bikes[Sp]) /*额外值大于最后一点所需值*/ 
			{
				extra=extra-Graph->Cmax/2+bikes[Sp]; /*额外值减去最后一点所需值就是back*/ 
				back=extra;
			}
			else
			{
				send=send+Graph->Cmax/2-bikes[Sp]-extra; /*send值加上最后一点所需值*/ 
			}
		}
		if(result->minsend>send) /*路径相同,比较send值*/ 
		{
			result->minnumber=i;  /*记录第几条路径最小*/ 
			result->minsend=send; /*更新最小send*/ 
			result->minback=back; /*更新最小back*/ 
		}
		else if(result->minsend==send)
		{
			if(result->minback>back) /*send相同,比较back值*/ 
			{
				result->minnumber=i;  /*记录第几条路径最小*/ 
				result->minback=back; /*更新最小back*/ 
			}
		}
	}
	return result; 
} 



贵校刚结束的project2,请自觉绕行。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值