国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

1003. Emergency (25)

2019-11-08 19:24:48
字體:
來源:轉載
供稿:網友
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.InputEach 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.OutputFor 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 Input5 6 0 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1Sample Output2 4
//一開始/*amount[start] = team[start];dist[start] = 0;pathcount[start] = 1;u = 0;dist[1] = dist[0] + map[0][1]; // dist[1] == 1dist[2] = dist[0] + map[0][1]; // dist[2] == 2amount[1] = 1 + 2;pathcount[1] = 1;pathcount[2] = 1;u = 1;dist[1] + map[1][2] == 2;//滿足dist[2] == dist[1] _ map[1][2];pathcount[2] = 1 + 1;//此時amount[2] = 0,amount[1] == 3,team[2] == 1;//soamount[2] = 3 + 1;....此時0~2的最短距離和0~2的所有team數目已經求出來了,分別是pathcount[2],amount[2]*/
#include <cstdio>#include <iostream>#include <cstdlib>using namespace std;const int MX = 500 + 5;const int INF = 9999999;int map[MX][MX];//記錄兩個點之間的距離sint dist[MX];//記錄從起始點到點i的距離int teams[MX];//記錄每個城市的隊伍數量int amount[MX];//記錄起始點到當前點的最大隊伍數量int pathcount[MX];//記錄從起始點到當前點點的最短路徑數量int v[MX];//標記是否被訪問過int N,M,start,en,dmin;void dijkstra(int start) {	amount[start] = teams[start];	dist[start] = 0;	pathcount[start] = 1;	while (1){        int u,dmin = INF;        //都是從0開始,因為第一遍循環會把起始點找出來        for (int i = 0; i < N; i++){            if (v[i] == 0 && dist[i] < dmin){                dmin = dist[i];                u = i;            }        }		//dmin==INF表示所有點已經都遍歷過了        if (dmin == INF)            break;		//將找到的點標記一下,說明這個點已經訪問過,之后就不要訪問了		v[u] = 1;		for (int i = 0; i < N; i++) {			if (v[i] == 0) {				if (dist[i] > dist[u] + map[u][i]) {					dist[i] = dist[u] + map[u][i];					amount[i] = amount[u] + teams[i];					pathcount[i] = pathcount[u];				}				else  if (dist[i] == dist[u] + map[u][i]) {					//如果距離相等就,到達當前的最短路徑數就加1                    pathcount[i] += pathcount[u];                    //如果之前的最大隊伍數量小于現在的隊伍數量,則進行更新                    if (amount[i] < amount[u] + teams[i]){                        amount[i] = amount[u] + teams[i];                    }				}			}		}	}	}int main() {	while (~scanf("%d%d%d%d",&N,&M,&start,&en)) {		for (int i = 0; i < N; i++) {			cin >> teams[i];		}		//初始化map數組		for (int i = 0; i < N; i++) {			dist[i] = INF;			for (int j = 0; j < N; j++) {				map[i][j] = INF;			}		}		//讀入數據		for (int i = 0; i < M; i++) {			int c1,c2,L;			cin >> c1 >> c2 >> L;			map[c1][c2] = map[c2][c1] = L;		}		//對所有點進行松弛		dijkstra(start);				cout << pathcount[en] << " " << amount[en] << endl;	}	system("pause");	return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 漯河市| 莱芜市| 沂南县| 襄樊市| 兴安盟| 闸北区| 丹棱县| 海南省| 安顺市| 株洲市| 五原县| 大连市| 华宁县| 砀山县| 米脂县| 青浦区| 新邵县| 察哈| 福州市| 本溪市| 东平县| 武威市| 隆德县| 徐闻县| 孝感市| 陈巴尔虎旗| 星子县| 隆尧县| 乐东| 海林市| 碌曲县| 南和县| 紫云| 普兰县| 黄大仙区| 尼勒克县| 聂拉木县| 项城市| 桓仁| 潞城市| 雷州市|