[kuangbin带你飞]专题四 最短路练习 C

本文介绍了一个寻找从起点到终点的最大承载重量路径的问题。利用优先队列实现了一种改进的Dijkstra算法,该算法能有效找到有向图中任意两点间路径上的最小权重的最大值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://poj.org/problem?id=1797
Description
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

题意:

给定一个双向可达的有向图,求出1->n之间的所有可达路径,每条路径中的最小边的最大值。

tip:

上一个题是最长边最小,这个是最短边最大。。
最大堆,贪心的策略是每次的dis为前面路径的中最小值,但因为每次取出的都是最大值,故最后得到的一定正确。。
为毛线都是dijstra。。这个输出啊 excitd
\n\

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <deque>
#include <utility>
#include <functional>
const int maxn = 100010;
const double eps = 1e-6;
const int INF = (1<<30);
using namespace std;
int n,m,tot;
int dis[maxn],first[maxn];
typedef pair<int,int>pii;
priority_queue <pii> pq;

struct node{
    int v,w,next;
}edges[10*maxn];

void add(int u,int v,int w){
    edges[tot].v = v;edges[tot].w = w;edges[tot].next = first[u];first[u] = tot++;
    edges[tot].v = u;edges[tot].w = w;edges[tot].next = first[v];first[v] = tot++;
}

void dij(){
    while(!pq.empty()){
        int no = pq.top().second;
        if(no == n) return;
        pq.pop();

        for(int i = first[no]; i != -1;i = edges[i].next){
            if(min(edges[i].w ,dis[no]) > dis[edges[i].v]){
                dis[edges[i].v] = min(edges[i].w ,dis[no]);
                pq.push(make_pair(dis[edges[i].v],edges[i].v));
            }
        }
    }
}

void init(){
    tot = 0;
    memset(dis,0,sizeof(dis));
    memset(first,-1,sizeof(first));
    scanf("%d%d",&n,&m);
    for(int i = 0 ; i < m ; i++){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        add(u,v,w);
    }

    while(!pq.empty()){
        pq.pop();
    }
    dis[1] = INF;
    pq.push(make_pair(0,1));
}

int main(){
    int T;
    scanf("%d",&T);
    for(int ca = 1 ;ca <= T ;ca++){
        init();
        dij();
        printf("Scenario #%d:\n%d\n\n",ca,dis[n]);
    }
}
### 关于 kuangbin ACM 算法竞赛培训计划 #### 数论基础专题介绍 “kuangbin专题涵盖了数论基础知识的学习,旨在帮助参赛者掌握算法竞赛中常用的数论概念和技术。该系列不仅提供了丰富的理论讲解,还推荐了一本详细的书籍《算法竞赛中的初等数论》,这本书包含了ACM、OI以及MO所需的基础到高级的数论知识点[^1]。 #### 并查集应用实例 在另一个具体的例子中,“kuangbin”的第五个专题聚焦于并查集的应用。通过解决实际问题如病毒感染案例分析来加深理解。在这个场景下,给定一组学生及其所属的不同社团关系图,目标是从这些信息出发找出所有可能被传染的学生数目。此过程涉及到了如何高效管理和查询集合成员之间的连通性问题[^2]。 #### 搜索技巧提升指南 对于简单的搜索题目而言,在为期约两周的时间里完成了这一部分内容的学习;尽管看似容易,但对于更复杂的状况比如状态压缩或是路径重建等问题,则建议进一步加强训练以提高解题能力[^3]。 ```python def find_parent(parent, i): if parent[i] == i: return i return find_parent(parent, parent[i]) def union(parent, rank, x, y): rootX = find_parent(parent, x) rootY = find_parent(parent, y) if rootX != rootY: if rank[rootX] < rank[rootY]: parent[rootX] = rootY elif rank[rootX] > rank[rootY]: parent[rootY] = rootX else : parent[rootY] = rootX rank[rootX] += 1 # Example usage of Union-Find algorithm to solve the virus spread problem. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值