Heavy Transportation POJ 1797 最短路变形

Heavy Transportation POJ 1797 最短路变形

题意

原题链接

题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... nm条路,每条路都有相应的承重能力,然后让你求从编号为1的城市到编号为n的城市的路线中,最大能经过多重的车。

解题思路

这个题可以使用最短路的思路,不过转移方程变了\(dis[j]=max(dis[j], min(dis[u], e[u][j]))\)。这里dis[j]表示从标号为1的点到达编号为j的点的路径中,最小的承重能力,就像短板效应样,一个木桶所能容纳的水是由最短的木板决定的。

代码实现

//使用优先队列优化的Dijkstra算法
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=1e3+7;
const int inf=0x3f3f3f3f;
struct edge{
    int to, cost;
};
struct node{
    int d, u;
    friend bool operator<(const node a, const node b){
        return  a.d < b.d;
    }
};
int dis[maxn];
bool vis[maxn];
vector<edge> g[maxn];
priority_queue<node> que;
int t, n, m;
void init()
{
    for(int i=1; i<=n; i++){
        g[i].clear();
        vis[i]=0;
        dis[i]=-inf;
    }
    while(!que.empty()) que.pop();
}
void dij(int s)
{
    int u, num=0;
    edge e;
    dis[s]=inf;
    node tmp={inf, s}, next;
    que.push(tmp);
    while(!que.empty() && num<=n)
    {
        tmp=que.top();
        que.pop();
        u=tmp.u;
        if(vis[u]) continue;
        vis[u]=1;
        num++;
        for(int i=0; i<g[u].size(); i++)
        {
            e=g[u][i];
            if(!vis[e.to] && dis[e.to] < min(dis[u], e.cost) )
            {
                dis[e.to]=min(dis[u], e.cost);
                next.d=dis[e.to];
                next.u=e.to;
                que.push(next);
            }
        }
    }
}
int main()
{
    int cnt=1;
    scanf("%d",&t);
    while(t--)
    {
        int u, v;
        edge e;
        scanf("%d%d", &n, &m);
        init();
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d", &u, &v, &e.cost);
            e.to=v;
            g[u].push_back(e);
            e.to=u;
            g[v].push_back(e);
        }
        dij(1);
        printf("Scenario #%d:\n", cnt++);
        printf("%d\n\n", dis[n]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/alking1001/p/12041211.html

时间: 2024-10-10 05:51:28

Heavy Transportation POJ 1797 最短路变形的相关文章

POJ 1797 Heavy Transportation【Dijkstra最短路变形】

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 29682   Accepted: 7919 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

poj1797 - Heavy Transportation(最大边,最短路变形spfa)

题目大意: 给你以T, 代表T组测试数据,一个n代表有n个点, 一个m代表有m条边, 每条边有三个参数,a,b,c表示从a到b的这条路上最大的承受重量是c, 让你找出一条线路,要求这条线路上的最大的承重, 在所有其他线路最小. 题目分析: 这里只要将spfa进行一下变形就可以解决这问题了. 首先 我们的dist数组,起点位置要初始化为 INF, 其他位置初始化为 0 然后我们更新 dist 数组, 结果输出 dist[n]就行了 为什么这样写: 因为我们每次要找 所有路径中的最大边的最小一个,

C - Heavy Transportation POJ - 1797

BackgroundHugo 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 w

Heavy Transportation POJ - 1797

#include<iostream> #include<algorithm> #include<queue> #include<map> #include<cstring> #include<cstring> #include<cstdio> #include<math.h> using namespace std; const int N=1e6+100; int h[N],w[N],ne[N],e[N],i

POJ 1797 最短路

链接: http://poj.org/problem?id=1797 题意: 给出N个城市M条边,每条边都有个容量,求一条运输路线,使城市1到城市N的运输量最大 代码: 31 int cost[MAXN][MAXN]; 32 int d[MAXN]; 33 int used[MAXN]; 34 int n, m; 35 36 void dijkstra() { 37 memset(d, 0, sizeof(d)); 38 memset(used, 0, sizeof(used)); 39 d[0

POJ 3013最短路变形....

DES:计算输的最小费用.如果不能构成树.输出-1.每条边的费用=所有的子节点权值*这条边的权值.计算第二组样例可以知道树的费用是所有的节点的权值*到根节点的最短路径的长度. 用dij的邻接矩阵形式直接MLE.编译都通不过.换邻接表的形式.然后....模板... #include<stdio.h> #include<string.h> #include<stdio.h> #include<iostream> #include<queue> usi

POJ 1797 Heavy Transportation (最短路变形)

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 20364   Accepted: 5401 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

POJ 1797 Heavy Transportation(最大生成树/最短路变形)

传送门 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 31882   Accepted: 8445 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever

POJ 1797 Heavy Transportation (Dijkstra变形)

F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand busines