POJ 3662

来源:互联网 发布:玉溪香烟软件 编辑:程序博客网 时间:2024/04/29 13:41

    一道神奇的二分搜索结合最短路判可行性的题。
    这道题首先是二分枚举答案,然后对答案进行验证,所以用这种方法做题必须要有一种能够满足时间限制的可行性判断方法。这里同样是二分枚举答案,不过这道题要注意不要枚举出不存在的长度,所以要在一个记录了已有长度的有序数组上进行二分搜索。
    接下来就是本题的重点——可行性判断。首先要对图进行转化,如果边的长度大于枚举的值,则赋值为1,否则赋值为0。然后在该图上求起点1到终点N的最短路,如果距离小于等于K,则说明存在一条路径,John 只需支付L,否则意味着枚举的值不满足题目要求。
    顺便说一下,题目中“the length of”意思是“……的长度”,而“k lengths of”意思是“k段……”,千万注意。


代码(G++):

#include <iostream>#include <cstdio>#include <queue>#include <cstring>#include <algorithm>#define MAXP 10003#define MAXN 1003#define INF 20000using namespace std;typedef pair<int, int> pii;struct Edge{    int v;    int len;    int w;} edges[MAXP*2];int n, p, k, c, head[MAXN], next[MAXP*2], length[MAXP];void add_edge(int u, int v, int len){    edges[c].v = v;    edges[c].len = len;    next[c] = head[u];    head[u] = c;    c++;}void dijkstra(int s, int dist[]){    priority_queue<pii, vector<pii>, greater<pii> > pq;    pii nd;    int u, v;    fill(dist+1, dist+n+1, INF);    dist[s] = 0;    pq.push(make_pair(dist[s], s));    while(!pq.empty())    {        nd = pq.top();        pq.pop();        u = nd.second;        if(dist[u] < nd.first) continue;        for(int k=head[u]; k!=-1; k=next[k])        {            v = edges[k].v;            if(dist[v] > dist[u] + edges[k].w)            {                dist[v] = dist[u] + edges[k].w;                pq.push(make_pair(dist[v], v));            }        }    }//for(int i=1; i<=n; i++) cout<<dist[i]<<endl;}bool is_ok(int L){    int dist[MAXN];    for(int i=0; i<c; i++)    {        if(edges[i].len > L) edges[i].w = 1;        else edges[i].w = 0;    }    dijkstra(1, dist);    if(dist[n] <= k) return true;    else return false;}int main(){    int lb, ub, mid, a, b, e;    while(scanf("%d %d %d", &n, &p, &k) != EOF)    {        c = 0;        memset(head, -1, sizeof(head));        for(int i=0; i<p; i++)        {            scanf("%d %d %d", &a, &b, &length[i]);            add_edge(a, b, length[i]);            add_edge(b, a, length[i]);        }        if(n == 1)        {            printf("%d\n", 0);            continue;        }        length[p] = 0;        length[p+1] = -1;        sort(length, length+p+2);        e = unique(length, length+p+2) - length;//cout<<e<<endl;        lb = 0;        ub = e;        while(ub - lb > 1)        {            mid = (lb+ub) / 2;            if(is_ok(length[mid])) ub = mid;            else lb = mid;        }        if(ub == e) printf("-1\n");        else printf("%d\n", length[ub]);    }    return 0;}

题目:
Telephone Lines
Time Limit: 1000MS Memory Limit: 65536K

DescriptionFarmer 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 LiOutput* 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 Input5 7 11 2 53 1 42 4 83 2 35 2 93 4 74 5 6Sample Output4
0 0
原创粉丝点击