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.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.

Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

Sample Input

1
3 3
1 2 3
1 3 4
2 3 5

Sample Output

Scenario #1:
4

解題報告:

1:和https://mp.csdn.net/console/editor/html/104326392 這題差不多。

2:可以說是做題5分鐘,調bug一整天。這句話g[--u][--v] = g[v][u] = min(g[v][u], s)搬的模板。在一開始初始化g數組爲0,然後不管怎麼輸入都沒答案。=.=後來改爲s就過了。說下初始化問題:g數組要初始化爲0。dis數組除dis[0] = INF外,其餘都爲0。但我有個小疑問,當n = 1, m = 0時,答案是多少?

3:下面代碼1是Dijkstra堆優化模板。g:保存地圖。dis:源點到任意一點的可行路徑的最小邊的最大值。visit:標記數組。point結構體中的dis:距離,id:每個點的編號。Q:優先隊列,dis大的排在前面。

4:下面代碼2是SPFA的模板。g:保存地圖。cnt:每個點的入隊次數。dis:源點到任意一點的可行路徑的最小邊的最大值。visit:標記數組。point結構體中的dis:距離,id:每個點的編號。Q:隊列。

代碼1:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long ll;
const ll N = 1000+10;
const ll INF = 0xffffffff;
ll g[N][N], dis[N];
bool visit[N];
struct point{
    ll dis, id;
    bool operator < (point x)const {
        return dis < x.dis;
    }
}p1, p2;
priority_queue<point> Q;
void init(ll n, ll m){
    for(ll i=0; i<n; ++i)
        for(ll j=0; j<n; ++j)g[i][j] = 0;
    while(!Q.empty())Q.pop();
    for(ll i=0; i<m; ++i){
        ll u, v, s;
        scanf("%lld%lld%lld", &u, &v, &s);
        g[--u][--v] = g[v][u] = s;
    }
    for(ll i=0; i<n; ++i)dis[i] = 0, visit[i] = 0;
}
void Dijkstra(ll s, ll n){
    p1.id = s, p1.dis = 0, dis[s] = INF;
    Q.push(p1);
    while(!Q.empty()){
        p1 = Q.top();
        Q.pop();
        if(visit[p1.id])continue;
        visit[p1.id] = 1;
        for(ll i=0; i<n; ++i){
            if(!visit[i] && min(dis[p1.id], g[p1.id][i]) > dis[i]){
                dis[i] = p2.dis = min(dis[p1.id], g[p1.id][i]);
                p2.id = i, Q.push(p2);
            }
        }
    }
}
int main(){
    ll t, n, m, cas = 1;
    scanf("%lld", &t);
    while(t--){
        scanf("%lld%lld", &n, &m);
        init(n, m);
        Dijkstra(0, n);
        printf("Scenario #%lld:\n%lld\n\n", cas++, dis[n-1]);
    }
    return 0;
}

代碼2:

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long ll;
const ll N = 1000+10;
const ll INF = 0x3f3f3f3f;
ll g[N][N], cnt[N], dis[N];
bool visit[N];
struct point{
    ll id, dis;
}p1, p2;
queue<point> Q;
void init(ll n, ll m){
    for(ll i=0; i<n; ++i)
        for(ll j=0; j<n; ++j)g[i][j] = 0;
    for(ll i=0; i<n; ++i)dis[i] = 0, cnt[i] = 0, visit[i] = 0;
    while(!Q.empty())Q.pop();
    for(ll i=0; i<m; ++i){
        ll u, v, s;
        scanf("%lld%lld%lld", &u, &v, &s);
        g[--u][--v] = g[v][u] = s;
    }
}
bool SPFA(ll s, ll n){
    p1.id = s, p1.dis = 0, dis[s] = INF, cnt[s] = 1;
    Q.push(p1);
    while(!Q.empty()){
        p1 = Q.front();
        Q.pop(), visit[p1.id] = 0;
        for(ll i=0; i<n; ++i){
            if(min(dis[p1.id], g[p1.id][i]) <= dis[i])continue;
            p2.dis = dis[i] = min(dis[p1.id], g[p1.id][i]);
            if(visit[i])continue;
            visit[i] = 1, p2.id = i, cnt[i]++;
            if(cnt[i] > n)return 0;
            Q.push(p2);
        }
    }
    return 1;
}
int main(){
    ll t, n, m, cas = 1;
    scanf("%lld", &t);
    while(t--){
        scanf("%lld%lld", &n, &m);
        init(n, m);
        if(SPFA(0, n))printf("Scenario #%lld:\n%lld\n\n", cas++, dis[n-1]);
    }
    return 0;
}

 

發佈了211 篇原創文章 · 獲贊 9 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章