C Dijkstra expands to include node weight 1003 Emergency (25 points)

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

Solve the problem of
Dijkstra deformation;
travel planning is to add more weights;
this problem is to increase the weight of nodes-the number of rescue teams; in
addition, it is necessary to calculate how many shortest paths;

 How many shortest paths are required

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

 The shortest path requiring the fewest edges

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

Difficult
point 1 Initialize dist as the shortest distance from the starting point to each point, initialized as INFINITY ;
collected is whether to enter the point, the initial is False ;
count is the number of shortest paths from the starting point to each point, the initial is 0 ;
teamcount is from The maximum number of rescue teams at the shortest distance from the starting point to each point, initially 0;

Initialize the dist at the starting point to 0 (the distance between yourself and yourself is 0); count to 1 (the shortest path from yourself to yourself is 1); teamcount is the number of rescue teams in the city;

Then start the DIjkstra cycle;
take the minimum distance not found, and perform n cycles

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];
		}

Difficulties 2 Calculate the number of rescue teams and the number of shortest paths
.
When finding a shorter path, the rescue team is the largest rescue team on the path to v plus the number of rescue teams in city i;
when dist [V] + Graph-> G [V] [i] == dist [i] When it occurs, there must be an edge between v and i;
so when comparing the number of rescue teams at point i, if v can be greater, then update to v and add i Rescue team; the
number of shortest paths
find a shorter path-then the shortest path to i is the same as the number of shortest paths to v;
find the same length path-then the path to i is itself + the path to v;

Dijkstra code

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];
 } 

Building function

#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 function

int main()
{
	int team[MAX];
	MGraph Graph=BuildGraph(team);
	Dikjstra(Graph, team);
}
Published 105 original articles · won praise 6 · views 4959

Guess you like

Origin blog.csdn.net/BLUEsang/article/details/105452312