LightOJ1002---Country Roads (最短路变形)

限时加码!20+主流AI编程工具免费用 购周边加赠Coding Plan Lite,Claude Code、Cursor等即刻畅享,学习进阶更高效! 阅读详情

I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from 0 to n-1 and each road has a cost. There are m roads. You are given the number of my city t where I belong. Now from each city you have to find the minimum cost to go to my city. The cost is defined by the cost of the maximum road you have used to go to my city.

For example, in the above picture, if we want to go from 0 to 4, then we can choose

1) 0 - 1 - 4 which costs 8, as 8 (1 - 4) is the maximum road we used

2) 0 - 2 - 4 which costs 9, as 9 (0 - 2) is the maximum road we used

3) 0 - 3 - 4 which costs 7, as 7 (3 - 4) is the maximum road we used

So, our result is 7, as we can use 0 - 3 - 4.
Input

Input starts with an integer T (≤ 20), denoting the number of test cases.

Each case starts with a blank line and two integers n (1 ≤ n ≤ 500) and m (0 ≤ m ≤ 16000). The next m lines, each will contain three integers u, v, w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 20000) indicating that there is a road between u and v with cost w. Then there will be a single integer t (0 ≤ t < n). There can be multiple roads between two cities.
Output

For each case, print the case number first. Then for all the cities (from 0 to n-1) you have to print the cost. If there is no such path, print ‘Impossible’.
Sample Input

Output for Sample Input

2

5 6

0 1 5

0 1 4

2 1 3

3 0 7

3 4 6

3 1 8

1

5 4

0 1 5

0 1 4

2 1 3

3 4 7

1

Case 1:

4

0

3

7

7

Case 2:

4

0

3

Impossible

Impossible
Note

Dataset is huge, user faster I/O methods.

dijkstra算法变形

/*************************************************************************
  > File Name: LightOJ1002.cpp
  > Author: ALex
  > Mail: zchao1995@gmail.com
  > Created Time: 2015年06月02日 星期二 19时34分01秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int N = 555;
int dist[N];
priority_queue < PLL, vector<PLL>, greater<PLL> > qu;
struct node {
    int nxt;
    int w;
    int to;
}edge[40000];
bool vis[555];
int head[N], tot;

void addedge(int from, int to, int w) {
    edge[tot].to = to;
    edge[tot].w = w;
    edge[tot].nxt = head[from];
    head[from] = tot++;
}

void Dijkstra(int u, int n) {
    while (!qu.empty()) {
        qu.pop();
    }
    for (int i = 0; i < n; ++i) {
        dist[i] = inf;
    }
    dist[u] = 0;
    qu.push(make_pair(dist[u], u));
    while (!qu.empty()) {
        PLL tmp = qu.top();
        qu.pop();
        int s = tmp.second;
        int d = tmp.first;
        for (int i = head[s]; ~i; i = edge[i].nxt) {
            int t = edge[i].to;
            int w = edge[i].w;
            if (dist[t] > max(d, w)) {
                dist[t] = max(d, w);
                qu.push(make_pair(dist[t], t));
            }
        }
    }
}

int main() {
    int t, icase = 1;
    scanf("%d", &t);
    while (t--) {
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; ++i) {
            head[i] = -1;
        }
        tot = 0;
        int u, v, w;
        for (int i = 1; i <= m; ++i) {
            scanf("%d%d%d", &u, &v, &w);
            addedge(u, v, w);
            addedge(v, u, w);
        }
        scanf("%d", &u);
        Dijkstra(u, n);
        printf("Case %d:\n", icase++);
        for (int i = 0; i < n; ++i) {
            if (dist[i] == inf) {
                printf("Impossible\n");
            }
            else {
                printf("%d\n", dist[i]);
            }
        }
    }
    return 0;
}
LightOJ - Country Roads(短路变形) 题目链接:http://lightoj.com/volume_showproblem.php?problem=1002Time Limit:3 second(s)Memory Limit:32 MB Problem Description I am going to my home. There are many cities and many bi-directional roads ... 阅读详情

相关推荐

Light OJ - 1002 - Country Roads 题解

不是很难的题目,却搞得我好头痛,这里是应该了变形的Dijstra算法了,因为这里求短路径不是把所有路径相加,而是把路径上所有的值取大值,这个大值越小,就说明结果优,也不管你绕了多长的路。 另外值得注意的就是memset这个函数,看来很多人栽跟斗啊,这个函数只能用来清零,是不能用来初始化值的,因为它是按照字符来初始化值的,所以计算好也可以给整形清零,但是这次我用来给bool数组清零,结...

编程语言小筑 222

LightOJ - 1002(迪杰斯特拉求大之的小值)

LightOJ - 1002 题意:求s点到t点所有路径中大值的小值(简单化了)。 解法:迪杰斯特拉求相同路径上的大值,不同路径上的小值。 #include&lt;stdio.h&gt; #include&lt;string.h&gt; #include&lt;queue&gt; #include&lt;map&gt; #include&lt;iostream&gt; #incl...

zxy160的博客 307

LightOJ 1002

I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from0 to n-and each road has a cost. There are m roads. You are given the number of m...

几许情愁的博客 253

lightOJ 1002 小瓶颈树 ..

第一次要把小生成树保存下来。还是对算法理解有问题。一开始的时候要把起始点设置为1,并把所有的相连边加入队列中。 自己写edge的时候不喜欢记录来源,所以用了个Pair,实在捉鸡,可以直接写成带有from的struct,这样代码复杂度会减少很多。 #include #include #include #include #include #include using namespace st

z3635363的专栏 701

LightOJ 1002 Country Roads 短路变形

题目:http://www.lightoj.com/volume_showproblem.php?problem=1002 题意:给定无向图,求从某点出发到所有的点的短路径,本题中两点间短路径的定义为:两点间所有路径中长边的小值 思路:短路变形,求短路时稍微变一下条件即可 #include #include #include #include #include #def

弱智就要多努力 457

短路LightOJ 1002 - Country Roads 变形

点击打开链接 求每一点到N点的路径上大的一条边权 #include #include #include #include #include #include #include #include using namespace std; #include #include #include #include #include #include #define cle

Kewowlo 837

Lightoj1002——Country Roads短路变形

I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from 0 to n-1 and each road has a cost. There are m roads. You are given the number of m

Beyond the Sora 1432

LightOJ 1002 Country Roads(短路变形(SPFA||贝尔曼))

Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1002 Description I am going to my home. There are many cities and many bi

Grit_ICPC的博客 857

lightoj 1002 - Country Roads短路变形

1002 - Country Roads     PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB I am going to my home. There are many cities and many bi-di

CillyB的博客 701

Country Roads LightOJ 1002(短路变形)

I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from 0 to n-and each road has a cost. There are m roads. You are given the number of ...

galesaur_wcy 361

lightOJ 1002 country roads短路算法的拓展变式)

lightOJ 1002 Country Roads 单源长路径怎么求 ?当然就是把单源短路径改过来就行了. 那么,其他涉及边的状态转移的问题呢?是否也能用短路算法魔改而成?答案是肯定的. 题目大意 给定一张无向图和一个终点,定义一个点到终点的权为这个点到终点的所有路径中长度大的边的小值. 如图,点0到4的路径有三条,它们的路径上的边权大值分别是8,9,7,取小,输出7 题目分析...

仙都一普通市民的blog 302

lightoj-1002Country Roads(dijkstra变形)

light1002:传送门 【题目大意】 n个点m条边,给一个源点,找出源点到其他点的‘短路’ 定义:找出每条通路中大的cost,这些大的cost中找出一个小的即为‘短路’,dijkstra变形。dis[i]为s->i的‘短路’ 1 #include<bits/stdc++.h> 2 int mp[505][505],dis[505]...

ayci54153的博客 195

Light oj 1002 - Country Roads 迪杰斯特拉变形

Light oj 1002 - Country Roads 题目链接 , click here …1002 - Country Roads PDF (English) Statistics ForumTime Limit: 3 second(s) Memory Limit: 32 MBI am going to my home. There are many cities a

不念过往 , 不愧恒心 660

lightoj 1002 Country Roads (Dijkstra变形)

lightoj 1002 Country Roads (Dijkstra变形)

Tc的专栏 2033

LightOJ-1002-Country Roads [短路][Dijkstra]

题目传送门题意:给定无向图,求一个城市t到其他城市的低成本,成本的定义为路径中的大消费。思路:这个题跟POJ-2253-Frogger类似,可以用Dijkstra算法变形来写。也可以用kruskal来写。#include <bits/stdc++.h>using namespace std;int n, m; int mp[550][550]; const int INF = 0x3f3f3f3

裤头是叫裤头的裤头 406

lightoj1002 - Country Roads

light1002:传送门 大意: n个点m条边,给一个源点,找出源点到其他点的‘短路’ 定义:找出每条通路中大的cost,这些大的cost中找出一个小的即为‘短路’,dij变形。dis[i]为s->i的‘短路’ #include int mp[505][505],dis[505],vis[505]; using namespace std; int n; void dij

Lesroad 273

Light oj 1002 Country Roads (Dijkstra)

题目连接:   http://www.lightoj.com/volume_showproblem.php?problem=1002 题目描述:   有n个城市,从0到n-1开始编号,n个城市之间有m条边,中心城市为t,问每个城市到中心城市的小路径的花费,路径花费大小的定义为:一条路上花费大的边的值。 解题思路:   Dijkstra的变形,用Dijkstra求出来的单源路径可以保证...

weixin_30374009的博客 135

LIGHTOJ 1002COUNTRY ROADS 【求大边小的路径】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1002dij简单变形;代码:#include<stdio.h> #include<iostream> #include<math.h> #include<stdlib.h> #include<ctype.h> #include<al

mfcheer 484
上一篇: hdu2227---Find the nondecreasing subsequences(dp+树状数组)
下一篇: LightOJ1003---Drunk(拓扑排序判环)
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值