標籤:hdu1598
find the most comfortable roadTime Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3686 Accepted Submission(s): 1565
Problem DescriptionXX星有許多城市,城市之間通過一種奇怪的高速公路SARS(Super Air Roam Structure---超級空中漫遊結構)進行交流,每條SARS都對行駛在上面的Flycar限制了固定的Speed,同時XX星人對 Flycar的“舒適度”有特殊要求,即乘坐過程中最高速度與最低速度的差越小乘坐越舒服 ,(理解為SARS的限速要求,flycar必須瞬間提速/降速,痛苦呀 ),
但XX星人對時間卻沒那麼多要求。要你找出一條城市間的最舒適的路徑。(SARS是雙向的)。
Input輸入包括多個測試執行個體,每個執行個體包括:
第一行有2個正整數n (1<n<=200)和m (m<=1000),表示有N個城市和M條SARS。
接下來的行是三個正整數StartCity,EndCity,speed,表示從表面上看StartCity到EndCity,限速為speedSARS。speed<=1000000
然後是一個正整數Q(Q<11),表示尋路的個數。
接下來Q行每行有2個正整數Start,End, 表示尋路的起終點。
Output每個尋路要求列印一行,僅輸出一個非負整數表示最佳路線的舒適度最高速與最低速的差。如果起點和終點不能到達,那麼輸出-1。
Sample Input
4 41 2 22 3 41 4 13 4 221 31 2
Sample Output
10
題意:求聯通路中的最小速度差。
題解:先按邊權從小到大排序,再用並查集枚舉。
#include <stdio.h>#include <string.h>#include <limits.h>#include <algorithm>#define maxn 202#define maxm 1002using std::sort;using std::min;int pre[maxn];struct Node{int u, v, cost;} E[maxm];int ufind(int k){int a = k, b;while(pre[k] != -1) k = pre[k];while(a != k){b = pre[a];pre[a] = k;a = b;}return k;}bool cmp(Node a, Node b){return a.cost < b.cost;}int main(){int n, m, q, a, b, i, j, x, y, ans;while(scanf("%d%d", &n, &m) == 2){for(i = 0; i < m; ++i)scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].cost);sort(E, E + m, cmp);scanf("%d", &q);while(q--){scanf("%d%d", &a, &b);ans = INT_MAX;for(i = 0; i < m; ++i){memset(pre, -1, sizeof(pre));for(j = i; j < m; ++j){x = ufind(E[j].u);y = ufind(E[j].v);if(x != y) pre[x] = y;if(ufind(a) == ufind(b)){ans = min(ans, E[j].cost - E[i].cost);break;}}}if(ans == INT_MAX) printf("-1\n");else printf("%d\n", ans);}}return 0;}