Poj 1797 Heavy Transportation【Dijkstra变形】

本文介绍了一个寻找城市中最大载重路径的问题,通过一种类似于迪杰斯特拉算法的方法,找到从起点到终点的最大承重能力,确保巨型起重机可以安全运输。

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

Heavy Transportation

Time Limit: 3000MS

 

Memory Limit: 30000K

Total Submissions: 28249

 

Accepted: 7548

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

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

Source

TUD Programming Contest 2004, Darmstadt, Germany

 

题目大意:首先输入一个整数t,表示测试 数据组数,接下来每一组第一行输入两个整数n,m。表示一共有n个节点,m条边(不明白为什么是无向边,写有向边就是Wa,无向边就是Ac),表示从节点a,到节点b最大载货量为c。问从节点1出发,选择一条路径,到达n,使得能够送得最大的货量为多少。


思路:

1、首先初始化dis【i】=0;

2、每一次选择dis【i】最大的节点(当然是没有选过的),向外拓展,此点记做u。

3、枚举选取到的点的能够到达的点,记做v,其路径权值为w,那么不难推出松弛式:dis【v】=max(dis【v】,tmp);tmp=min(dis【u】,w)【dis【u】!=0】;tmp=w【dis【u】==0】;

4、将所有点都选取过了之后,输出dis【n】;


AC代码:


#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int head[100000];
struct EdgeNode
{
    int to;
    int w;
    int next;
}e[1000000];
int dis[100000];
int vis[100000];
int n,m,cont;
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void Dij()
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)dis[i]=0;
    for(int i=1;i<=n;i++)
    {
        int k;
        int tmp=-1;
        for(int j=1;j<=n;j++)
        {
            if(vis[j]==0&&dis[j]>tmp)
            {
                k=j;
                tmp=dis[j];
            }
        }
        vis[k]=1;
        for(int j=head[k];j!=-1;j=e[j].next)
        {
            int v=e[j].to;
            int w=e[j].w;
            if(vis[v]==0)
            {
                int tt=min(dis[k],w);
                if(tt==0)tt=w;
                dis[v]=max(dis[v],tt);
            }
            else if(v==k&&n==1)//解决n==1时候可能存在的自环问题
            {
                int tt=min(dis[k],w);
                if(tt==0)tt=w;
                dis[v]=max(dis[v],tt);
            }
        }
    }
}
int main()
{
    int kase=0;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cont=0;
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            add(x,y,w);
            //add(y,x,w);
        }
        Dij();
        printf("Scenario #%d:\n",++kase);
        printf("%d\n",dis[n]);
        printf("\n");
    }
}
/*
5
5 6
1 3 4
3 5 7
3 5 7
1 5 9
1 2 3
2 5 100
*/







POJ 1797是一道经典的图论题目,题目名称为“Heavy Transportation”。这道题目主要考察的是最大生成树算法,特别是Kruskal算法或Prim算法。以下是该题目的简要介绍和解决思路: ### 题目描述 给定一个无向图,图中有N个节点和M条边。每条边都有一个重量。你的任务是找到一条从节点1到节点N的路径,使得路径上最小重量的边尽可能大。 ### 输入格式 第一行包含一个整数T,表示测试用例的数量。 每个测试用例的第一行包含两个整数N和M,分别表示节点的数量和边的数量。 接下来的M行,每行包含三个整数A, B和C,表示节点A和节点B之间有一条重量为C的边。 ### 输出格式 对于每个测试用例,输出一行,包含一个整数,表示从节点1到节点N的路径上最小重量的边的最大可能值。 ### 解题思路 1. **最小生成树(Kruskal算法)**:我们可以将问题转化为求最小生成树的最大边权。由于我们需要找到从节点1到节点N的路径上最小重量的边尽可能大,因此我们可以对所有边按重量从大到小排序,然后依次加入图中,直到节点1和节点N连通为止。 2. **Prim算法**:我们也可以使用Prim算法来解决这个问题。Prim算法是从一个起始节点开始,逐步扩展生成树,每次选择与当前生成树相连的最小边,直到所有节点都被包含在生成树中。 ### 示例代码(Kruskal算法) ```java import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main { static int[] parent; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); for (int t = 1; t <= T; t++) { int N = scanner.nextInt(); int M = scanner.nextInt(); Edge[] edges = new Edge[M]; for (int i = 0; i < M; i++) { edges[i] = new Edge(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()); } Arrays.sort(edges, new Comparator<Edge>() { @Override public int compare(Edge e1, Edge e2) { return e2.weight - e1.weight; } }); parent = new int[N + 1]; for (int i = 1; i <= N; i++) { parent[i] = i; } int result = 0; for (Edge edge : edges) { if (find(edge.u) == find(edge.v)) continue; union(edge.u, edge.v); if (find(1) == find(N)) { result = edge.weight; break; } } System.out.println("Scenario #" + t + ":"); System.out.println(result); System.out.println(); } scanner.close(); } static int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } static void union(int x, int y) { parent[find(x)] = find(y); } static class Edge { int u, v, weight; Edge(int u, int v, int weight) { this.u = u; this.v = v; this.weight = weight; } } } ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值