1003. Emergency

本文介绍了一种用于规划救援队伍从当前城市到达需救援城市的最短路径算法,并在此过程中尽可能多地召集救援人员。该算法首先使用Dijkstra算法寻找两点间的最短路径,然后通过深度优先搜索(DFS)确定所有最短路径及这些路径上可召集的最大救援人数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

 

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Sample Output
2 4



ac代码:
//注意初始化时将所有e[i][j]所有初始化为max.不需要区分i=j;
#include<stdio.h>
#include<string.h>
const int vmax=510;
int dist[vmax];
int visit[vmax];
int n,c1,c2,per,count,maxper;//点数
int path[vmax];
int distance;//纪录到此结点能招到的人数;
typedef struct
{
	int v[vmax];
	int e[vmax][vmax];
	int vnumb;
	int enumb;
}graph;

void dfs(graph *g,int i)
{
//	int ii,jj;
//	visit[i]=1;
	if(distance>dist[c2]) return;
//	per+=g->v[i];
//	printf("%d\n",i);
//	int pp;
    if(i==c2) 
	{
		if(distance==dist[c2])
		{
		count++;
		if(maxper<per)
			maxper=per;
		}
		return;
	}
	
	
	
	for(int ii=0;ii<g->vnumb;ii++)
	{
		if(i!=ii&&g->e[i][ii]!=-1&&visit[ii]==0)
		{
		    per+=g->v[ii];
			distance+=g->e[i][ii];
			visit[ii]=1;
			dfs(g,ii);
			distance-=g->e[i][ii];
			visit[ii]=0;
			per-=g->v[ii];
		}
	}
}
			



void dijkstra(graph *g,int i)
{

    int q,p;
	memset(visit,0,sizeof(visit));
	int u=i;
	visit[u]=1;
	dist[u]=0;
//	path[u]=u;

	for(p=1;p<n;p++)
	{
	  for(q=0;q<n;q++)
	  {
		if(q!=u&&g->e[u][q]!=-1&&dist[q]>dist[u]+(g->e[u][q])&&visit[q]==0)//找u,q之间存在边,且q!=u,未被访问,且距离更小
		{
			dist[q]=dist[u]+g->e[u][q];
			path[q]=u;
		}
	  }
	   int minval=9999999;//find the min dist[u];不可以赋初值为dist[0];注意,可能dist[0]就是最小的,就死循环了。;
		for(q=0;q<n;q++)
		{
			if(visit[q]==0&&dist[q]<minval)//未被访问过的。易忘;
			{
				u=q;
				minval=dist[q];
			}
		}
		visit[u]=1;
	}
}
			


	




int main()
{
//	freopen("data.in","r",stdin);
	graph *g;
	g=new graph;
	int m,i,j,k,t;
	scanf("%d%d%d%d",&n,&m,&c1,&c2);
	g->vnumb=n;
	g->enumb=m;
    for(i=0;i<n;i++)//init...
		for(j=0;j<n;j++)
		{
			if(i!=j) g->e[i][j]=-1;
			else g->e[i][j]=0;
		}
		for(i=0;i<n;i++)
		{
			dist[i]=99999999;
			path[i]=i;
		}


	for(i=0;i<n;i++)
	{
		scanf("%d",&g->v[i]);
	}
	for(i=0;i<m;i++)
	{
	scanf("%d%d%d",&j,&k,&t);
//	printf("%d\n",t);
	g->e[j][k]=t;
	g->e[k][j]=t;
	}
/*
for(i=0;i<n;i++)
{
		for(j=0;j<n;j++)
		{
			printf("%d ",g->e[i][j]);
		}
		printf("\n");
}
*/




	dijkstra(g,c1);
 //   for(i=0;i<n;i++)
//	printf("i=%d,dist[%d]=%d,path[%d]=%d\n",i,i,dist[i],i,path[i]);
    count=0;
	maxper=0;
	distance=0;
	per=g->v[c1];
	visit[c1]=1;
	memset(visit,0,sizeof(visit));
	dfs(g,c1);
    printf("%d %d\n",count,maxper);
	return 0;
}

 

 




转载于:https://www.cnblogs.com/scjyldq/archive/2012/10/03/2710724.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值