POJ 1797 Heavy Transportation

Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
題意:先輸入測試組數,再輸n,m,即點數和邊數。m行,每行三個數據,兩個頂點及頂點的邊的容量。求1到n路徑最少容量的最大值。
跟上一篇一樣,dijkstra的變形。

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<cmath>
#include<functional>
using namespace std;
#define N 1000+5
#define MAXN 1000000
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define INF 0x3f3f3f3f
#define LL long long int 
#define pow(a) (a)*(a)
int d[N];
struct node{
    int x, y;
}p[N];
int vis[N];
int cost[N][N];
int n, m;
int t;
int cnt = 1;
void dijkstra(){
    mem(d, 0);
    mem(vis, 0);
    while (1){
        int v = -1;
        for (int i = 1; i <= n; i++){
            if (!vis[i] && (v == -1 || d[i] > d[v]))v = i;
        }
        if (v == -1)break;
        vis[v] = 1;
        for (int i = 1; i <= n; i++){
            if (!vis[i]){
                if(!d[i])d[i] = min((d[v]?d[v]:INF), cost[v][i]);
                else d[i] = max(d[i], min((d[v] ? d[v] : INF), cost[v][i]));
            }
        }
    }
    printf("Scenario #%d:\n%d\n\n", cnt++, d[n]);
}
int main(){
    cin >> t;
    while (t--){
        cin >> n >> m;
        mem(cost, 0);
        for (int i = 1; i <= m; i++){
            int a, b, c;
            cin >> a >> b >> c;
            cost[a][b] = cost[b][a] = max(c,cost[a][b]);
        }
        dijkstra();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章