首页 > 代码库 > poj 3662 Telephone Lines

poj 3662 Telephone Lines

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

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 Nis 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 11 2 53 1 42 4 83 2 35 2 93 4 74 5 6

Sample Output

4

Source

USACO 2008 January Silver
/** @Author: Lyucheng* @Date:   2017-07-24 11:08:55* @Last Modified by:   Lyucheng* @Last Modified time: 2017-07-24 17:36:49*//* 题意:题意很迷,有n个点,p条边,现在要修一条从1到n的线路,你可以将其中的k条线权值变为0,并且你修的线路中    最长的边,一定要是最小的 思路:二分答案,将大于mid的边权值为1,小于等于的为0,找出最少耗费的1,如果小于k那么就可以通过*/#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>#define MAXN 1005#define INF 0x3f3f3f3f#define MAXR 1000000using namespace std;int n,p,k;int u,v,w;struct HeapNode //Dijkstra算法用到的优先队列的节点{    int d,u;    HeapNode(int d,int u):d(d),u(u){}    bool operator < (const HeapNode &rhs)const    {        return d > rhs.d;    }};struct Edge     //{    int from,to,dist;    Edge(int f,int t,int d):from(f),to(t),dist(d){}};struct Dijkstra{    int n,m;            //点数和边数,编号都从0开始    vector<Edge> edges; //边列表    vector<int> G[MAXN];//每个节点出发的边编号(从0开始编号)    bool done[MAXN];    //是否已永久标号    int d[MAXN];        //s到各个点的距离    int p[MAXN];        //p[i]为从起点s到i的最短路中的最后一条边的编号    void init(int n)    {        this->n=n;        for(int i=0;i<n;i++) G[i].clear();//清空邻接表        edges.clear();  //清空边列表    }    void AddEdge(int from,int to,int dist)    {//如果是无向图,每条无向边调用两次AddEdge        edges.push_back(Edge(from,to,dist) );        m = edges.size();        G[from].push_back(m-1);    }    bool dijkstra(int s,int pos)//求s到所有点的距离    {        priority_queue<HeapNode> Q;        for(int i=0;i<n;i++) d[i]=INF;        d[s]=0;        memset(done,0,sizeof(done));        Q.push(HeapNode(0,s) );        while(!Q.empty())        {            HeapNode x=Q.top(); Q.pop();            int u=x.u;            if(done[u]) continue;            done[u]= true;            for(int i=0;i<G[u].size();i++)            {                Edge& e= edges[G[u][i]];                int new_dist=e.dist>pos?1:0;                if(d[e.to]> d[u]+new_dist)                {                    d[e.to] = d[u]+new_dist;                    p[e.to] = G[u][i];                    Q.push(HeapNode(d[e.to],e.to) );                }            }        }        if(d[n-1]>k) return false;        else return true;    }}DJ;int main(){    // freopen("in.txt", "r", stdin);    // freopen("out.txt", "w", stdout);    while(scanf("%d%d%d",&n,&p,&k)!=EOF){        DJ.init(n);        for(int i=0;i<p;i++){            scanf("%d%d%d",&u,&v,&w);            DJ.AddEdge(u-1,v-1,w);            DJ.AddEdge(v-1,u-1,w);        }        int res=-1;        int l=0,r=MAXR,m;        while(l<=r){            m=(l+r)/2;            if(DJ.dijkstra(0,m)==true){//如果这个长度可以完成的话                r=m-1;                res=m;            }else{//如果不可以完成的话                l=m+1;            }        }        printf("%d\n",res);    }    return 0;}

 

poj 3662 Telephone Lines