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 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: [email protected]
  > 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;
}
时间: 2024-11-05 02:20:14

LightOJ1002---Country Roads (最短路变形)的相关文章

zoj 1655 Transport Goods (最短路变形)

Transport Goods Time Limit: 2 Seconds      Memory Limit: 65536 KB The HERO country is attacked by other country. The intruder is attacking the capital so other cities must send supports to the capital. There are some roads between the cities and the

UVA - 10537 The Toll! Revisited (最短路变形逆推)

Description Problem G Toll! Revisited Input: Standard Input Output: Standard Output Time Limit: 1 Second Sindbad the Sailor sold 66 silver spoons to the Sultan of Samarkand. The selling was quite easy; but delivering was complicated. The items were t

URAL 1934 Black Spot --- 简单最短路变形

边权为1,在维护最短路的同时维护p值最小,我直接存的(1-p),即不遇见的概率,要使得这个值最大. #include <iostream> #include <cstdlib> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #includ

UESTC 915 方老师的分身II --最短路变形

即求从起点到终点至少走K条路的最短路径. 用两个变量来维护一个点的dis,u和e,u为当前点的编号,e为已经走过多少条边,w[u][e]表示到当前点,走过e条边的最短路径长度,因为是至少K条边,所以大于K条边的当做K条边来处理就好了.求最短路的三个算法都可以做,我这里用的是SPFA,比较简洁. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #incl

zoj1655 最短路变形

题意:HERO过的首都需要货物,需要从其他的城市吧货物送到首都,每条道路都会需要消耗一定比例的货物,问最多能送多少货物到首都. 思路:如果每个点的比例是1,到达首都的比例就是经过的路径的(1-消耗比)的乘积,反正是无向的,所以可以反过来推,首都的货物比是1,而到达每座 城市的货物就是所经过的路径(1-消耗比)的乘积,则由此可见,我们可以求首都到任意城市的最大比值:最后把每个点的最大比值乘以每个点的货物加起来 即是结果. #include<stdio.h> #include<string.

HN0I2000最优乘车 (最短路变形)

HN0I2000最优乘车 (最短路变形) [试题]为了简化城市公共汽车收费系统,某城市决定对大部分的公共汽车都采用一票制,但由于某些公共汽车所经过的停车站太多和路途太长,就采用两票或多票制.经过这种票制改革后,人们坐公共汽车从一个站到另一个站时,就不得不选择一个好的乘车方案,以使他们的乘车费用最低. 为了方便于求出最佳的乘车方案,我们假设: l  采用一票制的公共汽车,无论从哪个站上车到那个站下车,乘该公共汽车的费用为1(费用单位). l  采用多票制的公共汽车,将设立某些站为关键站:那么,如果

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

HDOJ find the safest road 1596【最短路变形】

find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9033    Accepted Submission(s): 3173 Problem Description XX星球有非常多城市,每一个城市之间有一条或多条飞行通道,可是并非全部的路都是非常安全的,每一条路有一个安全系数s,s是在 0 和

HDU 4318 图论之最短路变形

点击打开链接 题意:看样例说把,一共4个点,接下来输入一个数,代表当前点连接的点的数量,然后那几个点,样例里面就是1->2消耗50%的能量,1->3消耗70%的能量,2->1消耗30%的能量,2->4消耗20%的能量,一次类推,最后一行输入起始位置,重点位置,开始时的能量,问从起点走到终点,消耗的能量最少. 思路:很明显的最短路变形,我们可以将求出从起点到终点剩余的最多的能量,用总能量减去它,就是消耗的最少的能量,这就好办了,将最短路改成最长路即可,但是每次比较时就是比较乘积而不是