【C++】1003. Emergency (25)

1003. Emergency (25)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

1.关于求图的最小路径算法 

1.只通过一个测试点。

#include<iostream>
#include<vector>
using namespace std;

struct ROAD{
	int L;
	int to;
};

std::vector<std::vector<ROAD>> map;


int main(){
	int hh,mm;
	freopen("in.txt","r",stdin);
	int N,M,C1,C2,n,i;
	while(scanf("%d%d%d%d",&N,&M,&C1,&C2)!=EOF){
		int nn=C1;
		n=N;
		int dist[501];
		//memset(dist,-1,sizeof(int));
		int rescue[501]={0},cost[501]={0};
		bool mark[501]={false};
		for(i=0;i<N;i++){
			int temp;
			scanf("%d",&temp);
			rescue[i]=temp;
			dist[i]=-1;
		}
		map.resize(N);
		for(i=0;i<M;i++){
			int c1,c2,len;
			ROAD aa;
			scanf("%d%d%d",&c1,&c2,&len);
			aa.L=len;
			aa.to=c2;
			map[c1].push_back(aa);
			aa.to=c1;
			map[c2].push_back(aa);
		}

		
		int j;
		mark[C1]=true;
		dist[C1]=0;
		//cost[C1]=rescue[C1];
		for(i=0;i<N;i++){
			for(j=0;j<map[nn].size();j++){//
				int t=map[nn][j].to;
				int ll=map[nn][j].L;
				//int co=map[nn][j].cost;
				if(mark[t]==true){
					continue;	
				}
				if(dist[t]==-1||dist[t]>dist[nn]+ll||(dist[t]==dist[nn]+ll&&cost[t]<cost[nn]+rescue[nn])){
					dist[t]=dist[nn]+ll;
					cost[t]=rescue[nn]+cost[nn];
				}
			}
			int min=123123123;
			for(int j=0;j<N;j++){//找出最小的路
				if(mark[j]==true) continue;
				if(dist[j]<min&&dist[j]>=0){
					min=dist[j];
					nn=j;
				}
			}
			mark[nn]=true;
		}
		cout<<dist[C2]<<" "<<cost[C2]+rescue[C2];
	}
	
	return 0;
}


【C++】1003. Emergency (25),布布扣,bubuko.com

【C++】1003. Emergency (25)

上一篇:工厂模式——《java与模式》


下一篇:python数据结构与算法 15 队列应用之烫手的山芋