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 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.

Input

* Line 1: Three space-separated integers: N, P, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: Ai, Bi, and Li

Output

*
Line 1: A single integer, the minimum amount Farmer John can pay. If it
is impossible to connect the farm to the phone company, print -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
【分析】题意有点绕口我这里就不说了,防止说错,说说做法吧。学长们都是二分+dij过得,我是用的分层图。分层图专门用来解决这种免费问题的,然后d[x][k]表示到x节点用了k次免费
的最优解,然后跑spfa。
#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#include <set>
#include <queue>
#include <string>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef pair<int,int>pii;
typedef long long LL;
const int N=2e3+;
const int mod=1e9+;
int n,m,s,k,t,cnt,idl[N<<],idr[N<<];
bool vis[N][];
int d[N][];
vector<pii>edg[N];
struct man{
int v;
int c;
int w;
bool operator<(const man &e)const{
return w>e.w;
}
};
priority_queue<man>q;
void spfa(int s){
memset(d,-,sizeof d);memset(vis,,sizeof vis);
d[s][]=;
q.push(man{s,,});
while(!q.empty()){
int u=q.top().v,c=q.top().c;q.pop();
if(vis[u][c])continue;
vis[u][c]=;
for(int i=;i<edg[u].size();++i){
int v=edg[u][i].first,w=edg[u][i].second;
if(!vis[v][c]&&(d[v][c]==-||d[v][c]>max(d[u][c],w))){
d[v][c]=max(d[u][c],w);
q.push(man{v,c,d[v][c]});
}
if(c<k){
if(!vis[v][c+]&&(d[v][c+]==-||d[v][c+]>d[u][c])){
d[v][c+]=d[u][c];
q.push(man{v,c+,d[v][c+]});
}
}
}
}
}
int main()
{
int x,y,w;
scanf("%d%d%d",&n,&m,&k);
s=;t=n;
while(m--)
{
scanf("%d%d%d",&x,&y,&w);
edg[x].push_back(make_pair(y,w));
edg[y].push_back(make_pair(x,w));
}
spfa(s);
int ans=;
for(int i=;i<=k;i++)ans=min(ans,d[t][i]);
printf("%d\n",ans);
return ;
}

POJ 3662 Telephone Lines (分层图)的更多相关文章

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

    题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total ...

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

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 D ...

  3. [USACO08JAN]电话线Telephone Lines(分层图)/洛谷P1948

    这道题其实是分层图,但和裸的分层图不太一样.因为它只要求路径总权值为路径上最大一条路径的权值,但仔细考虑,这同时也满足一个贪心的性质,那就是当你每次用路径总权值小的方案来更新,那么可以保证新的路径权值 ...

  4. poj 3662 Telephone Lines spfa算法灵活运用

    意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...

  5. poj 3662 Telephone Lines

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7115   Accepted: 2603 D ...

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

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6973   Accepted: 2554 D ...

  7. poj 3662 Telephone Lines dijkstra+二分搜索

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accepted: 2071 D ...

  8. poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)

    Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...

  9. POJ 3662 Telephone Lines(二分答案+SPFA)

    [题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...

随机推荐

  1. 原生js addclass,hasClass,removeClass,toggleClass的兼容

    (function (window) { 'use strict'; // class helper functions from bonzo https://github.com/ded/bonzo ...

  2. Codeforces Round #483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!]

    题目链接:http://codeforces.com/contest/984 A. Game time limit per test:2 seconds memory limit per test:5 ...

  3. 完全教程 Aircrack-ng破解WEP、WPA-PSK加密利器

    其 实关于无线基础知识的内容还是挺多的,但是由于本书侧重于BT4自身工具使用的讲解,若是再仔细讲述这些外围的知识,这就好比讲述DNS工具时还要把 DNS服务器的类型.工作原理及配置讲述一遍一样,哈哈, ...

  4. Perl6 Bailador框架(6):获取用户输入

    use v6; use Bailador; get '/' => sub { ' <html> <head><title></title>< ...

  5. Java21个基础知识点

    类装载器就是寻找类的字节码文件,并构造出类在JVM内部表示的对象组件.在Java中,类装载器把一个类装入JVM中,要经过以下步骤: (1) 装载:查找和导入Class文件: (2) 链接:把类的二进制 ...

  6. java===java基础学习(13)---this,static(静态变量和静态方法)的使用

    package dog; public class PersonAndDog { public static void main(String[] args) { Dogs da_huang = ne ...

  7. 64_g6

    gsettings-desktop-schemas-devel-3.24.0-1.fc26.x..> 22-Mar-2017 20:46 19386 gsf-sharp-0.8.1-27.fc2 ...

  8. C# 使用HttpWebRequest Post提交数据,携带Cookie和相关参数示例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. mapper.xml中的<sql>标签

    原文链接:http://blog.csdn.net/a281246240/article/details/53445547 sql片段标签<sql>:通过该标签可定义能复用的sql语句片段 ...

  10. scrapy框架搭建与第一个实例

    scrapy是python的一个网络爬虫框架,关于它的介绍有很多资料,这里不做过多介绍(好吧我承认我还不是很懂...).我现在还在摸索阶段,因为用scrapy爬取的第一个网站非常简单,不涉及登陆.验证 ...