[USACO 08JAN]Telephone Lines

Description

Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system.

There are N (1 ≤ N ≤ 1,000) forlorn telephone poles conveniently numbered 1..N that are scattered around Farmer John‘s property; no cables connect any them. A total of P (1 ≤ P ≤ 10,000) pairs of poles can be connected by a cable; the rest are too far apart.

The i-th cable can connect the two distinct poles Ai and Bi, with length Li (1 ≤ Li ≤ 1,000,000) units if used. The input data set never names any {Ai, Bi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and N need to be connected by a path of cables; the rest of the poles might be used or might not be used.

As it turns out, the phone company is willing to provide Farmer John with K (0 ≤ K < N) lengths of cable for free. Beyond that he will have to pay a price equal to the length of the longest remaining cable he requires (each pair of poles is connected with a separate cable), or 0 if he does not need any additional cables.

Determine the minimum amount that Farmer John must pay.

多年以后,笨笨长大了,成为了电话线布置师。由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人。该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有p(1<=p<=10000)对电话杆可以拉电话线。其他的由于地震使得无法连接。

第i对电线杆的两个端点分别是ai,bi,它们的距离为li(1<=li<=1000000)。数据中每对(ai,bi)只出现一次。编号为1的电话杆已经接入了全国的电话网络,整个市的电话线全都连到了编号N的电话线杆上。也就是说,笨笨的任务仅仅是找一条将1号和N号电线杆连起来的路径,其余的电话杆并不一定要连入电话网络。

电信公司决定支援灾区免费为此市连接k对由笨笨指定的电话线杆,对于此外的那些电话线,需要为它们付费,总费用决定于其中最长的电话线的长度(每根电话线仅连接一对电话线杆)。如果需要连接的电话线杆不超过k对,那么支出为0.

请你计算一下,将电话线引导震中市最少需要在电话线上花多少钱?

Input

输入文件的第一行包含三个数字n,p,k;

第二行到第p+1行,每行分别都为三个整数ai,bi,li。

Output

一个整数,表示该项工程的最小支出,如果不可能完成则输出-1.

Sample Input

5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6

Sample Output

4

题解

和这道题差不多:[JLOI 2011]飞行路线&[USACO 09FEB]Revamping Trails

只不过求最短路变成求瓶颈路。

 1 //It is made by Awson on 2017.10.26
 2 #include <set>
 3 #include <map>
 4 #include <cmath>
 5 #include <ctime>
 6 #include <queue>
 7 #include <stack>
 8 #include <cstdio>
 9 #include <string>
10 #include <vector>
11 #include <cstring>
12 #include <cstdlib>
13 #include <iostream>
14 #include <algorithm>
15 #define LL long long
16 #define Max(a, b) ((a) > (b) ? (a) : (b))
17 #define Min(a, b) ((a) < (b) ? (a) : (b))
18 using namespace std;
19 const int N = 1000;
20 const int M = 10000;
21
22 int n, p, K, u, v, c, INF;
23 struct tt {
24   int to, cost, next;
25 }edge[(M<<1)+5];
26 int path[N+5], top;
27 int dist[N+5][N+5];
28 bool vis[N+5][N+5];
29 struct node {
30   int u, k;
31   node() {
32   }
33   node(int _u, int _k) {
34     u = _u, k = _k;
35   }
36 };queue<node>Q;
37
38 void add(int u, int v, int c) {
39   edge[++top].to = v;
40   edge[top].cost = c;
41   edge[top].next = path[u];
42   path[u] =top;
43 }
44 int SPFA() {
45   memset(dist, 127/3, sizeof(dist));
46   INF = dist[0][0];
47   for (int i = 0; i <= K; i++) {
48     dist[1][i] = 0, vis[1][i] = 1; Q.push(node(1, i));
49   }
50   while (!Q.empty()) {
51     int u = Q.front().u, k = Q.front().k; Q.pop(); vis[u][k] = 0;
52     for (int i = path[u]; i; i = edge[i].next) {
53       int v = edge[i].to;
54       if (k < K) {
55     if (dist[v][k+1] > dist[u][k]) {
56       dist[v][k+1] = dist[u][k];
57       if (!vis[v][k+1]) {
58         vis[v][k+1] = 1; Q.push(node(v, k+1));
59       }
60     }
61       }
62       if (dist[v][k] > Max(dist[u][k], edge[i].cost)) {
63     dist[v][k] = Max(dist[u][k], edge[i].cost);
64     if (!vis[v][k]) {
65       vis[v][k] = 1; Q.push(node(v, k));
66     }
67       }
68     }
69   }
70   if (dist[n][K] == INF) return -1;
71   return dist[n][K];
72 }
73 void work() {
74   scanf("%d%d%d", &n, &p, &K);
75   for (int i = 1; i <= p; i++) {
76     scanf("%d%d%d", &u, &v, &c);
77     add(u, v, c), add(v, u, c);
78   }
79   printf("%d\n", SPFA());
80 }
81 int main() {
82   work();
83   return 0;
84 }
时间: 2024-10-14 06:30:06

[USACO 08JAN]Telephone Lines的相关文章

poj 3662 Telephone Lines dijkstra+二分搜索

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accepted: 2071 Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of

POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of

poj 3662 Telephone Lines(最短路+二分)

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6973   Accepted: 2554 Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of

poj3662 Telephone Lines【最短路】【二分】

http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:9310   Accepted: 3374 Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative,

USACO08JAN Telephone Lines 题解

1.USACO08JAN  Telephone Lines 题面 由于问的是最大值最小,所以二分加验证就好了 比较显然的,题干问的是第k+1长的路最短: 那么二分答案是正确的方向: 但是怎么验证? 我们可以将所有边权大于二分的答案的边视为边权是1,否则看成0: 然后从1~n跑最短路,如果答案大于二分的答案那么就不成立,否则成立: 这种思维比较重要,代码还是很简单的: #include <bits/stdc++.h> using namespace std; int head[2000010],

[BZOJ] 1614: [Usaco2007 Jan]Telephone Lines架设电话线

1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1806  Solved: 773[Submit][Status][Discuss] Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1,000)根按1..N顺次编号的废

bzoj1614[Usaco2007 Jan]Telephone Lines架设电话线*

bzoj1614[Usaco2007 Jan]Telephone Lines架设电话线 题意: n个节点,1号节点已经连入互联网,现在需要将整个图连入网络.有K条边可以免费连接,最后总费用为所有连边费用的最大值,求最小总费用.n≤10000 题解: 二分费用,将连边费用大于二分值的长度记为1,否则记为0,求最短路,如果到某个点的距离超过k,则需要增加答案,继续二分. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include &l

POJ 3662 Telephone Lines (分层图)

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6785   Accepted: 2498 Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of

hust 1039 Telephone Lines

题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system. There are N (1 <= N <= 1,000) forlorn telep