POJ2395 Out of Hay 【Dijkstra】

Out of Hay

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11610   Accepted: 4535

Description

The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situation. There are N (2 <= N <= 2,000) farms (numbered 1..N); Bessie starts at Farm 1. She‘ll traverse some or all
of the M (1 <= M <= 10,000) two-way roads whose length does not exceed 1,000,000,000 that connect the farms. Some farms may be multiply connected with different length roads. All farms are connected one way or another to Farm 1.

Bessie is trying to decide how large a waterskin she will need. She knows that she needs one ounce of water for each unit of length of a road. Since she can get more water at each farm, she‘s only concerned about the length of the longest road. Of course, she
plans her route between farms such that she minimizes the amount of water she must carry.

Help Bessie know the largest amount of water she will ever have to carry: what is the length of longest road she‘ll have to travel between any two farms, presuming she chooses routes that minimize that number? This means, of course, that she might backtrack
over a road in order to minimize the length of the longest road she‘ll have to traverse.

Input

* Line 1: Two space-separated integers, N and M.

* Lines 2..1+M: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, describing a road from A_i to B_i of length L_i.

Output

* Line 1: A single integer that is the length of the longest road required to be traversed.

Sample Input

3 3
1 2 23
2 3 1000
1 3 43

Sample Output

43

Hint

OUTPUT DETAILS:

In order to reach farm 2, Bessie travels along a road of length 23. To reach farm 3, Bessie travels along a road of length 43. With capacity 43, she can travel along these roads provided that she refills her tank to maximum capacity before she starts down a
road.

Source

USACO 2005 March Silver

找生成树的瓶颈边。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;

#define maxn 2002
#define maxm 1010
#define inf 0x3f3f3f3f

int N, M, map[maxn][maxn], dist[maxn];
bool vis[maxn];

void getMap() {
    int i, j, u, v, d;
    memset(map, 0x3f, sizeof(map));
    memset(dist, 0x3f, sizeof(int) * (N + 1));
    while(M--) {
        scanf("%d%d%d", &u, &v, &d);
        if(map[u][v] > d) {
            map[u][v] = map[v][u] = d;
        }
    }
}

int getNext() {
    int u = -1, d = inf;
    for(int i = 1; i <= N; ++i)
        if(!vis[i] && dist[i] < d) {
            d = dist[i];
            u = i;
        }
    return u;
}

void Dijkstra() {
    int u, v, i, ans = 0;
    u = 1; dist[u] = 0;
    while(u != -1) {
        vis[u] = 1;
        ans = max(dist[u], ans);
        for(i = 1; i <= N; ++i)
            if(i != u) dist[i] = min(dist[i], map[u][i]);
        u = getNext();
    }
    printf("%d\n", ans);
}

int main() {
    // freopen("stdin.txt", "r", stdin);
    scanf("%d%d", &N, &M);
    getMap();
    Dijkstra();
    return 0;
} 
时间: 2024-08-27 20:12:04

POJ2395 Out of Hay 【Dijkstra】的相关文章

POJ2395 Out of Hay【Kruskal】

Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11656Accepted: 4562 Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situat

HDU2680 Choose the best route 【Dijkstra】

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7061    Accepted Submission(s): 2300 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

HDU1874 畅通工程续 【Dijkstra】

畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 26970    Accepted Submission(s): 9719 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走

HDU2544 最短路 【Dijkstra】

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 31182    Accepted Submission(s): 13456 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

HDOJ 1142 A Walk Through the Forest 【Dijkstra】+【DFS】

题意:从2到1的所有路径中找出最短的路,并且输出最短路径有几条. 策略:先求出最短路径,然后再找出从2到1的最短路径有几条.最短路径用dijkstra算法来求出,什么是dijkstra算法,简单来说,dijkstra算法就是路径长度递增次序产生最短路径的算法: 基本思想是:把集合V分成两组: (1)S:已求出最短路径的顶点的集合 (2)V-S=T:尚未确定最短路径的顶点集合 将T中顶点按最短路径递增的次序加入到S中, 保证:(1)从源点V0到S中各顶点的最短路径长度都不大于 从V0到T中任何顶点

HDOJ 1874 畅通工程续 【dijkstra】

题意:... 策略:最简单的求最短路径. 代码: #include<stdio.h> #include<string.h> #define MAXN 1005 #define INF 0x3f3f3f3f int di[MAXN], vis[MAXN], n, m; int map[MAXN][MAXN]; void dijkstra(int v) { int i, j; memset(vis, 0, sizeof(vis)); di[v] = 0; vis[v] = 1; for

HDU3790 最短路径问题 【Dijkstra】

最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13336    Accepted Submission(s): 4072 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. Input

HDU2112 HDU Today 【Dijkstra】

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14743    Accepted Submission(s): 3471 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候

ZOJ--1655--Transport Goods【dijkstra】

题意:某国首都正被攻打,需要运送物资到首都,告诉你n个点,编号1~n,n是首都,剩下的点各有wi重量的物资,m条路,每条路有个货物损失比例,现需要求出最多能运送多少货物到首都. 其实转换一下就是一个最短路问题,边的权值是损失比例,找损失比例最小的那条路,则能运送的货物最多. dist数组存放运成功的比例,初始化为0表示运不成. WA了N发,各种double类型都用int定义的,而且它给的样例即使定义成int对结果也没影响... #include<cstring> #include<str