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. Zyan 一个通信框架

    原文地址 本文示例 Zyan是一个简单直观的分布式应用程序开发框架. 以下是Zyan项目的简要概述. 架构图: Zyan一般由客户端和服务端组成.服务端(以组件的形式)提供服务,客户端远程调用服务端的 ...

  2. python+selenium+js 处理滚动条

    selenium并不是万能的,有时候页面上操作无法实现的,这时候就需要借助JS来完成了. 常见场景: 当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的. 这时候需要借 ...

  3. Linux查看进程的所有子进程和线程

    得到进程的pid: ps -ef | grep process_name | grep -v "grep" | awk '{print $2}' 查看进程的所有线程 # ps mp ...

  4. Freemarker <#list List/Map/Array[] as Object>

    http://blog.csdn.net/ani521smile/article/details/52164366 详细教程链接

  5. 【转载】Quick 中的触摸事件

    原文地址 http://cn.cocos2d-x.org/article/index?type=quick_doc&url=/doc/cocos-docs-master/manual/fram ...

  6. vue调试工具

    在进行vue项目开发的时候,免不了要进行调试,谷歌插件vue-devtools可以帮忙 步骤 步骤一: 到谷歌商店搜索"vue-devtools"下载 步骤二: 在chrome的扩 ...

  7. PHP路由代码

    <?php /**  * 路由  * @author 角度 QQ:1286522207  *  */ class Dispatcher extends Action {     private ...

  8. net_device->uc_promisc

    如果设备不支持单播过滤,并且要监听多个单播地址时,就要使用net_device->uc_count和net_device->uc_promisc来设置混杂模式,具体见__dev_set_r ...

  9. MediaWiki安装配置(Linux)【转】

    转自:http://blog.csdn.net/gao36951/article/details/43965527 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 1Media ...

  10. Development tools[重点]

    Development tools yum groupinfo "Development tools" Loaded plugins: product-id, security, ...