[二分-最小化最大值+最短路]poj-3662

原创 2018年04月16日 21:52:47
Telephone Lines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8159 Accepted: 2942

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 {AiBi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and 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: NP, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: AiBi, 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

题意:

求一条路径从1到n使第k+1大的边最小。因为我们可以使得前K大边免费
二分答案mid,当原边权小于等于mid新边权为0,否则新边权为1.
求最短路,若最短距离小于等于k说明mid满足条件。
代码:

最短路采用Spfa算法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e3+10;
int n,p,k;
struct Edge {
    int to,val;
    Edge(int _to=0,int _val=0):to(_to),val(_val){}
};
vector<Edge> way[maxn],g[maxn];
bool vis[maxn];
int dist[maxn];
void spfa()
{
    memset(vis,0,sizeof(vis));
    for(int i=0;i<maxn;i++) dist[i] = inf;
    dist[1] = 0;vis[1] = 1;
    queue<int> qu;
    qu.push(1);
    while(!qu.empty())
    {
        int u = qu.front();qu.pop();
        for(int i=0;i<g[u].size();i++) {
            Edge e = g[u][i];
            if(dist[u] + e.val < dist[e.to]) {
                dist[e.to] = dist[u] + e.val;
                if(!vis[e.to]) {
                    vis[e.to] = 1;
                    qu.push(e.to);
                }
            }
        }
        vis[u] = 0;
    }
}
void add_edge(int u,int v,int l) {
    Edge edge(v,l);
    way[u].push_back(edge);
    g[u].push_back(edge);
    Edge edge1(u,l);
    way[v].push_back(edge1);
    g[v].push_back(edge1); /// 这里写错了..调了巨多遍
}
void init() {
    for(int i=0;i<maxn;i++) way[i].clear(),g[i].clear();
}
bool check(int mid) {
    for(int i=1;i<=n;i++) {
        for(int j=0;j<way[i].size();j++) {
            if(way[i][j].val > mid) g[i][j].val = 1;
            else g[i][j].val = 0;
        }
    }
    spfa();
    return dist[n] <= k;
}
int main()
{
    while(~scanf("%d%d%d",&n,&p,&k)) {
        init();
        for(int i=0;i<p;i++) {
            int u,v,l;scanf("%d%d%d",&u,&v,&l);
            add_edge(u,v,l);
        }
        spfa();
        if(dist[n] == inf) {printf("-1\n");continue;}

        int left = 0,right = 1e6+10;
        while(left <= right) {
            int mid = (left + right)>>1;
            if(check(mid)) right = mid-1;
            else left = mid + 1;
        }
        printf("%d\n",left);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38013346/article/details/79967181

二分答案方法 — 最大值最小化问题

最大值最小化问题(分治解法) 把一个包含n个正整数的序列划分成m个连续的子序列(每个正整数恰好属于一个序列)。设i个序列的各数之和为S(i),你的任务是让所有S(i)的最大值尽量小 #...
  • u010258605
  • u010258605
  • 2015-01-31 10:03:38
  • 4279

POJ: Drying(二分、值最小化)

题目链接:POJ - 3104 题意:有n件湿衣服,分别给出其含水量。若衣服放在空气中,每分钟含水量会减少1;若把衣服放进烘干器里面则含水量每分钟会减少k,当衣服含水量为0时说明衣服已经干了,现...
  • lisong_jerry
  • lisong_jerry
  • 2017-07-26 22:20:10
  • 82

poj 3273 Monthly Expense (二分最小化最大值)

题目链接:http://poj.org/problem?id=3273
  • u013903504
  • u013903504
  • 2014-11-09 21:59:16
  • 504

[ACM] POJ 3258 River Hopscotch (二分,最大化最小值)

River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6697  ...
  • sr19930829
  • sr19930829
  • 2014-07-23 17:51:10
  • 1375

zzuli 1919 (二分 最大值最小化)

Description晴天想把一个包含n个整数的序列a分成连续的若干段,且和最大的一段的值最小,但他有强迫症,分的段数不能超过m段,然后他就不会分了。。。他想问你这个分出来的和最大的一段的和最小值是多...
  • xiaosshhaa
  • xiaosshhaa
  • 2016-09-06 13:21:06
  • 188

poj 3662 二分+最短路

题意: 给定一个n个点的无向图fr,to,w。 现在从1点到n点,有k条边可以免花费,求免费之后剩下的那些边里面最大花费最小的值是多少。 解析: 首先搞懂题意,从1点到n点可以有很多条...
  • u013508213
  • u013508213
  • 2015-08-02 19:54:52
  • 222

[二分-最小化最大值] - poj3104-Drying

DryingTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 20853 Accepted: 5246DescriptionIt is...
  • m0_38013346
  • m0_38013346
  • 2018-04-17 21:30:08
  • 4

POJ---3273(二分,最小化最大值)

和这道题一样了: Description 聪哥在暑假参加了打零工的活动,这个活动分为n个工作日,每个工作日的工资为Vi。有m个结算工钱的时间,聪哥可以自由安排这些时间,也就是说什么时候拿钱...
  • sgh666666
  • sgh666666
  • 2018-02-04 01:17:17
  • 53

二分查找中的最大值的最小值问题

挑战程序设计2的 allocation#include #include using namespace std; int a[100001]; int k,n; int panduan(int m...
  • qq_36616023
  • qq_36616023
  • 2017-07-18 20:11:38
  • 347
收藏助手
不良信息举报
您举报文章:[二分-最小化最大值+最短路]poj-3662
举报原因:
原因补充:

(最多只允许输入30个字)