ZOJ 1053 (Dijkstra 算法 最短路径 入门)

FDNY to the Rescue!

Time Limit: 2 Seconds Memory Limit: 65536 KB

The Fire Department of New York (FDNY) has always been proud of their response time to fires in New York City, but they want to make their response time even better. To help them with their response time, they want to make sure that the dispatchers know the closest firehouse to any address in the city. You have been hired to write this software and are entrusted with maintaining the proud tradition of FDNY. Conceptually, the software will be given the address of the fire, the locations of the firehouses, street intersections, and the time it takes to cover the distance between each intersection. It will then use this information to calculate how long it takes to reach an address from each firehouse.Given a specific fire location in the city, the software will calculate the time taken from all the fire stations located in the city to reach the fire location. The list of fire stations will be sorted from shortest time to longest time. The dispatcher can then pick the closest firestation with available firefighters and equipment to dispatch to the fire.

Input Format: Line 1: # of intersections in the city, a single integer (henceforth referred to as N) N<20Lines 2 to N+1: A table (square matrix of integer values separated by one or more spaces) representing the time taken in minutes between every pair of intersections in the city. In the sample input shown below the value ��3�� on the 1st row and the 2nd column represents the time taken from intersection #1 to reach intersection #2. Similarly the value ��9�� on the 4th row and the 2nd column represents the time taken from intersection #4 to reach intersection #2.A value of -1 for time means that it is not possible to go directly from the origin intersection (row #) to the destination intersection (column #). All other values in the table are non-negative.Line N+2: An integer value n (<= N) indicating the intersection closest to the fire location followed by one or more integer values for the intersections closest to the fire stations (all on one line, separated by one or more spaces) will follow the input matrix.Notes on input format: 1. The rows and columns are numbered from 1 to N. 2. All input values are integers 3. All fire locations are guaranteed reachable from all firehouses. 4. All distance calculations are made from the intersection closest to each firehouse to the intersection closest to the fire.

Output Format: Line 1: A label line with the headings for each column, exactly as shown in the example. Line 2 onwards (one line for each fire station): A sorted list (based on time) showing the fire station (origin), the destination site, time taken and a complete shortest path of nodes from the originating fire station to the fire location.Notes on output format: 1. Columns are tab separated. 2. If two or more firehouses are tied in time they can be printed in any order. 3. If more than one path exists that has the same minimal time for a given location & firehouse, either one can be printed on the output.4. If the fire location and the fire station locations happen to be the same intersection, the output will indicate that the origin and destination have the same intersection number, the time will be ��0�� and the nodes in the shortest path will show just one number, the fire location.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Sample Input: 16 0 3 4 -1 -1 -1 -1 0 4 5 -1 -1 2 3 0 -1 -1 2 8 9 5 0 1 -1 7 2 1 -1 0 -1 5 -1 4 5 4 0 2 4 5 6

In the above input the last line indicates that ��2�� is the location of the fire and ��4��, ��5�� and ��6�� are the intersections where fire stations are located.

Sample Output: Org Dest Time Path 5 2 2 5 24 2 3 4 5 2 6 2 6 6 5 2


Source: Mid-Atlantic USA 2001

code : c++

Runtime: 0

Runmemory: 180KB


//附上我的代码  我的第一题图论    就是看了一下图论算法书最短路径里面  dijkstra 的算法描述写来的
//其实整体思路没问题  才开始我用的map存储输出目标,结果网上一查 可以重复输出  于是改成队列存储
//之后陷入轮回般的WA中,主要自己没静下心来好好考虑
//其实最后仔细再次省察代码,就又发现了小错误没希望自己以后能够 耐下心来  好好做题    这是成为合格Acmer的  必要条件啊!
#include<stdio.h>
#include<string.h> 
#include<ctype.h> 
#include<queue>
using namespace std; 
const int maxn = 21;
const int INF = 1000000000; 
struct Node {
	int ti,x; 
	friend	bool operator<(Node a,Node b){
		 return a.ti>b.ti; 
	}	 
}; 

priority_queue<Node>Q; 
void Dijkstra(int n,int dist[maxn],int map[maxn][maxn],int pre[maxn],int s) 
{
	int i,j,k;
	int min;
	bool p[maxn];
	p[s]=true; 
	pre[s]=0; 
	for(i=1;i<=n;i++)
	{
		
		if(i!=s){
			p[i]=false;
			dist[i]=map[i][s];
			pre[i]=s; 
		}
	} 
	for(i=1;i<=n-1;i++)
	{
		min= INF;
		k=0; 
		for(j=1;j<=n;j++)
		{
			if(!p[j]&&dist[j]!=INF&&dist[j]<min)
			{
				min=dist[j];
				k=j; 
			} 
		}
		if(k==0) return;
		p[k]=true;
		for(j=1;j<=n;j++)
		{
			if(!p[j]&&map[j][k]!=INF&&dist[k]+map[j][k]<dist[j])
			{
				dist[j]= dist[k]+map[j][k];
				pre[j]=k;
			} 
		} 		
	} 
} 
void output(int x,int pre[])
{
	if(x!=0){
		printf("\t%d",x);
		output(pre[x],pre);		
	} 
} 
int main()
{
	 int dist[maxn],map[maxn][maxn],pre[maxn];
	 int t;
	 int n;
	 int num; 
	 int s;
	 char x[10000];  
	 int a[maxn]; 
	 Node p; 
	 scanf("%d",&t);
	 while(t--)
	 {
		 scanf("%d",&n);
		 for(int i=1;i<=n;i++)
		 	for(int j=1;j<=n;j++)
			 {
			 	scanf("%d",&map[i][j]);
			 	if(map[i][j]==-1) map[i][j]=INF;
			 } 
		 scanf("%d",&s);
		 num=0; 
		 getchar(); 
		 gets(x); 
		 int len=strlen(x);
		 int tmp=0; 
		 for(int i=0;i<=len;i++)
		 {
		 	if(isdigit(x[i]))  tmp=tmp*10+x[i]-'0';
			else if(tmp){
				a[num++]=tmp;
				tmp=0; 
			} 
		 } 
 		 
		 memset(pre,0,sizeof(pre));
		 memset(dist,0,sizeof(dist)); 
		 Dijkstra(n,dist,map,pre,s);
		 for(int i=0;i<num;i++)
		 {
		 	if(dist[a[i]]!=INF) 
		 	{
				 p.ti=dist[a[i]];
				 p.x=a[i]; 
				 Q.push(p); 
		 	} 
		 } 
		 printf("Org\tDest\tTime\tPath\n");	 
		 while(!Q.empty())
		 {
			  Node front = Q.top();
			  printf("%d\t%d\t%d\t%d",front.x,s,front.ti,front.x);
			  output(pre[front.x],pre); 
			  printf("\n"); 
			  Q.pop();	  
		 } 	
		 if(t!=0) printf("\n");	 
	 } 
	 return 0; 
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值