C Dijkstra拓展 含结点权重 1003 Emergency (25分)

47 篇文章 0 订阅
25 篇文章 0 订阅

1003 Emergency (25分)

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 Specification:
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 C​2​​ - 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 c​1, c​2 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 C​1 to C​2
​​ .

Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C​1 and C​2​​ , 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

解题
Dijkstra变形;
旅行规划是多了边权值;
该题是多了结点的权值——救援队的数量;
除此之外,还要计算有多少条最短路径;

 要求数最短路径有多少条

 count[s] = 1;
 如果找到更短路:count[W]=count[V];
 如果找到等长路:count[W]+=count[V];

 要求边数最少的最短路

 count[s] = 0;
 如果找到更短路:count[W]=count[V]+1;
 如果找到等长路:count[W]=count[V]+1;

难点1初始化
dist为起始点到各点的最短距离,初始化为INFINITY
collected为是否收入该点,初始为False
count为从起始点到各点的最短路径个数,初始为0
teamcount为从起始点到各点最短距离的最大救援队数,初始为0;

初始化起始点的dist为0(自己到自己距离为0);count为1(自己到自己的最短路径有1条);teamcount为该城市的救援队数;

然后开始DIjkstra循环;
取未被找到的距离最小值,进行n次循环

if(dist[V]+Graph->G[V][i]<dist[i])     
		{
			dist[i]=dist[V]+Graph->G[V][i];
			count[i]=count[V];
			teamcount[i]=teamcount[V]+teams[i];       
		}
		else if(dist[V]+Graph->G[V][i]==dist[i])     
		{
			count[i]+=count[V];
			if(teamcount[V]+teams[i]>teamcount[i]) 
				teamcount[i]=teamcount[V]+teams[i];
		}

难点2 计算救援队数量和最短路径数量
救援队数
当找到更短路径,救援队为到 v 的路径的最大救援队再加上 i 城的救援队数;
当 dist[V]+Graph->G[V][i]==dist[i] 发生时,v和i之间必有一条边;
故比较到 i 点的救援队数,若经过v可以更大,则更新为经过v再加i的救援队;
最短路径数
找到更短路径——则到 i 与到 v 的最短路径数相同;
找到相同长度路径——则到 i 的路径为自身 + 到 v 的路径;

Dijkstra代码

void Dikjstra(MGraph Graph, int *teams)
{
	int dist[MAX];          //初始化为边INFINITY 
	bool collected[MAX];      //初始化为false
	int count[MAX];         //初始化为0
	int teamcount[MAX];     //初始化为0 
	fill(dist,dist+MAX,INFINITY);
	fill(count,count+MAX,0);
	fill(teamcount,teamcount+MAX,0);
	memset(collected, false, sizeof(collected));
	
	dist[Graph->start]=0;
	count[Graph->start]=1;      //自己到自己只有一条
	teamcount[Graph->start]= teams[Graph->start];      
	
	while(1)
	{
		int V=-1;
		int min=INFINITY;
		for (int i = 0; i < Graph->Nv; ++i) {     //找到最小值  
            if(!collected[i]&&dist[i]<min){    //没有找到过且更小的 
                V=i;min=dist[i];       //保存下标v 
            }
        }
		if(V==-1) break;
		collected[V]=true;
		for(int i=0;i<Graph->Nv;i++)
		{
		if(!collected[i])
		{
			if(dist[V]+Graph->G[V][i]<dist[i])      //发生这个时——vi必有边 
			{
				dist[i]=dist[V]+Graph->G[V][i];
				count[i]=count[V];
				teamcount[i]=teamcount[V]+teams[i];       //V与i有边, 
			}
			else if(dist[V]+Graph->G[V][i]==dist[i])     //vi有边 
			{
				count[i]+=count[V];
				//计算救援队数量要把路径上的全部加起来 
				//比较经过V的和不经过V的哪个救援队数量多 
				if(teamcount[V]+teams[i]>teamcount[i]) 
					teamcount[i]=teamcount[V]+teams[i];
			}	
		}
		}	
	}		
			cout<<count[Graph->end]<<" "<<teamcount[Graph->end];
 } 

建图函数

#include<iostream>
#include <memory.h>
using namespace std;
#define MAX 501
#define INFINITY 65533

typedef struct ENode *ptrtoENode;
struct ENode {
	int V1;
	int V2;
	int length;
};
typedef ptrtoENode Edge;

typedef struct GNode *ptrtoGraph;
struct GNode{
	int Nv;
	int Ne;
	int G[MAX][MAX];
	int start;
	int end;
};
typedef ptrtoGraph MGraph;

MGraph CreateGraph(int N)
{
	MGraph Graph = new struct GNode;
	Graph->Nv=N;
	Graph->Ne=0;
	for(int i=0;i<Graph->Nv;i++)
		for(int j=0;j<Graph->Nv;j++)
			Graph->G[i][j]=INFINITY;
	return Graph;    //忘记写返回 
}

void InsertEdge(MGraph Graph,Edge E)
{
	Graph->G[E->V1][E->V2]=E->length;
	Graph->G[E->V2][E->V1]=E->length;
}

MGraph BuildGraph(int teams[])
{
	int N;
	cin>>N;
	MGraph Graph=CreateGraph(N);
	cin>>Graph->Ne>>Graph->start>>Graph->end;
	for(int i=0;i<Graph->Nv;i++)
	{
		cin>>teams[i];
	}
	for(int i=0;i<Graph->Ne;i++)
	{
		Edge E= new struct ENode;
		cin>>E->V1>>E->V2>>E->length;
		InsertEdge(Graph,E);
	}
	return Graph;
}   //得到图,并且填满teams

main函数

int main()
{
	int team[MAX];
	MGraph Graph=BuildGraph(team);
	Dikjstra(Graph, team);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值