POJ 1797-Heavy Transportation-dijkstra小变形和POJ2253类似

传送门:http://poj.org/problem?id=1797

题意:

在起点和终点间找到一条路,使得经过的边的最小值是最大的;

和POJ2253类似,传送门:http://www.cnblogs.com/ckxkexing/p/8747108.html

思路:

跑一边dijkstra,每次松弛的条件改为:if( dis[tmp] < min(dis[v],tmpc) )dis[tmp] = min( dis[v] , tmpc);

注意这道题和POJ2253的区别在于每次选取的节点要最小,即向优先队列中push入正的dis就OK;

2>这道题还可以用最小生成树来做;

#include <iostream>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define pb push_back
#define fi first
#define se second
typedef long long ll;

const ll INF = 1e12+9;
const int maxn  = 100007;

ll n, m, k, s;
ll dis[maxn];
bool vis[maxn];
vector < pair<ll, ll > > mp[maxn];
priority_queue< pair<ll ,ll > > q;

void dij(){
    memset(vis,0,sizeof(vis));
    while(!q.empty())
    {
            int v = q.top().se;
            q.pop();
            if(vis[v])continue;
            vis[v] = 1;
            for(int i=0;i<mp[v].size();i++)
            {
                int tmp = mp[v][i].fi;
                ll tmpc = mp[v][i].se;
                if(dis[tmp] < min(dis[v] , tmpc)||dis[tmp]==-1)
                {
                    dis[tmp] = min(dis[v] , tmpc);
                    q.push(make_pair(dis[tmp], tmp));
                }
            }
    }
}
int main(){
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;t++)
    {
        scanf("%lld%lld", &n,&m);

        for(int i = 1; i <= n; i ++)
            mp[i].clear(),dis[i]=-1;

        for(int i=1; i<=m; i++)
        {
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            mp[x].pb(make_pair(y,c));
            mp[y].pb(make_pair(x,c));
        }

        dis[1] = INF;
        q.push(make_pair(0,1));
        dij();
        printf("Scenario #%d:\n",t);
        printf("%lld\n\n",dis[n]);
    }
    return 0;
}

 

posted @ 2018-04-09 22:41  ckxkexing  阅读(103)  评论(0编辑  收藏  举报