洛谷 P1948 [USACO08JAN]电话线Telephone Lines 题解

P1948 [USACO08JAN]电话线Telephone Lines

题目描述

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.

多年以后,笨笨长大了,成为了电话线布置师。由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人。该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有p(1<=p<=10000)对电话杆可以拉电话线。其他的由于地震使得无法连接。

第i对电线杆的两个端点分别是ai,bi,它们的距离为li(1<=li<=1000000)。数据中每对(ai,bi)只出现一次。编号为1的电话杆已经接入了全国的电话网络,整个市的电话线全都连到了编号N的电话线杆上。也就是说,笨笨的任务仅仅是找一条将1号和N号电线杆连起来的路径,其余的电话杆并不一定要连入电话网络。

电信公司决定支援灾区免费为此市连接k对由笨笨指定的电话线杆,对于此外的那些电话线,需要为它们付费,总费用决定于其中最长的电话线的长度(每根电话线仅连接一对电话线杆)。如果需要连接的电话线杆不超过k对,那么支出为0.

请你计算一下,将电话线引导震中市最少需要在电话线上花多少钱?

输入格式

输入文件的第一行包含三个数字n,p,k;

第二行到第p+1行,每行分别都为三个整数ai,bi,li。

输出格式

一个整数,表示该项工程的最小支出,如果不可能完成则输出-1.

输入输出样例

输入 #1

5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6

输出 #1

4

【思路】

二分答案+SPFA

【题目大意】

点1到点n中间有很多对可以连接电线的电线杆子
连接的权值不一样
可以让你免费连接K条
求最少的花费(花费等于连接的最长的那个电线,这里连接的电线当然不包括免费的)

【题目分析】

答案是具有单调性的
因为如果花费i的钱可以的话
那花费i+1块钱也一定可以
花费i块钱不行的话
那花费i-1块钱也一定是不可以的
所以用二分答案就很显然了吧

【核心思路】

二分什么呢?
当然是二分花费了
因为是可以免去k条的
所以大于二分出来的值的那就变为0就好了
所以如果1到n被免去最少电线的那条路
只要满足免去电线的根数小于等于k
那这个答案就是属于可行范围的
反之则是不可行的
这就是二分的思路

不过,既然是求最小的那根
那为什么不用SPFA呢?
当成最短路来写就好了
被免去的边的权值就赋值为1
没有被免去的那就是0
这样就能求出1到n之间连接的边中
被免去最少边的那条边的免去的值是多少了

【题外话】

一开始我想的是二分出来值之后搜索的
然后搜索中如果某一条边不行的话
那应该返回去搜另一条边的
但是如果这个点之后的下一个点不行的话
那应该是搜索另外的和这个点连着的点的
所以应该是continue
但是我打成了return
还莫名其妙的比continue多了20分成了70分
因为这个玄学的return让我少跑了很多的东西导致不会超时
因为这个我被gyh大佬戏称为“最强优化大师”!
(在最后附上我的错误解法代码)

【完整代码——AC】

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int Max = 20010;
struct node
{
    int y,ne,z;
}a[Max];
int sum = 0,head[1010];
void add(int x,int y,int z)
{
    a[++ sum].y = y;
    a[sum].ne = head[x];
    a[sum].z = z;
    head[x] = sum; 
}
int n,p,k;
int d[1010];
bool use[1010];
bool check(int x)
{
    memset(d,0x3f,sizeof(d));
//  memset(use,false,sizeof(use));
//  memset(cnt,0,sizeof(cnt));
    queue<int>q;
    d[1] = 0;
    q.push(1);
    use[1] = true;
    while(!q.empty())
    {
        int qwq = q.front();
        q.pop();
        use[qwq] = true;
        for(register int i = head[qwq];i != 0;i = a[i].ne)
        {
            int owo;
            if(a[i].z > x)
            owo = d[qwq] + 1;
            else
            owo = d[qwq];
            if(owo < d[a[i].y])
            {
                d[a[i].y] = owo;
                if(!use[a[i].y])
                {
                    q.push(a[i].y);
                    use[a[i].y] = true;
                }
            }
        }
        use[qwq] = false;
    }
    if(d[n] <= k)return true;
    else    return false;
}

int main()
{
    cin >> n >> p >> k;
    for(register int i = 1;i <= p;++ i)
    {
        int u,v,w;
        cin >> u >> v >> w;
        add(u,v,w);add(v,u,w);
    }
    int ans = -1;
    int l = 0,r = 1000000;
    while(l <= r)
    {
        int mid = (r + l) >> 1;
        if(check(mid))r = mid - 1,ans = mid;
        else    l = mid + 1;
    }
    cout << ans << endl;
    return 0; 
}

【买一赠一 —— 错解】

/*
我二分出来最长的那条电话线的长度
然后从1开始搜索
如果这条线长度超出了二分出来的值
那就先用那k对免费的电话线
如果k对用完了那这条路就是行不通的
就回到上一个点换条路接着搜索
如果这个搜索里面有能够在满足刚好用完或者用不完k条免费的电话线的情况下
还能够让最长的电话线小于等于二分出来的数
那二分返回就是真  
*/
#include<iostream>
#include<cstdio>
#define int long long
using namespace std;
const int Max = 10004;
const int M = 1003;
struct node
{
    int y,ne;
    int w;
}a[Max << 1];
int sum = 0;
int head[M];
void add(int u,int v,int w)
{
    a[++ sum].y = v;
    a[sum].ne = head[u];
    a[sum].w = w;
    head[u] = sum;
    
    a[++ sum].y = u;
    a[sum].ne = head[v];
    a[sum].w = w;
    head[v] = sum; 
}
int n,p,k;
int fg;
bool use[M];

void dfs(int x,int fath,int js,int mm)
{
    if(fg == 0)
    {
        if(x == n)
        {
            fg = 1;
            return;
        }
        for(register int i =head[x];i != 0;i = a[i].ne)
        {
            if(a[i].y != fath && use[a[i].y] == false)
            {
                if(a[i].w > mm)
                {
                    if(js > 0)
                    {
                        use[a[i].y] = true;
                        dfs(a[i].y,x,js - 1,mm);    
                        use[a[i].y] = false;
                    }
                    else    return; 
                }
                else
                {
                    use[a[i].y] = true;
                    dfs(a[i].y,x,js,mm);    
                    use[a[i].y] = false;
                }
            }
        }
    }
}

bool check(int x)
{
    fg = 0;
    dfs(1,0,k,x);
    if(fg == 0)return false;
    else    return true;
}

int father[M];

int find(int x)
{
    if(father[x] != x)father[x] = find(father[x]);
    return father[x];
}

void hebing(int x,int y)
{
    x = find(x);
    y = find(y);
    if(x != y)
        father[x] = y;
}

signed main()
{
    cin >> n >> p >> k;
    int MM = 0; 
    for(register int i = 1;i <= n;++ i)
        father[i] = i;
    for(register int i = 1;i <= p;++ i)
    {
        int u,v,w;
        cin >> u >> v >> w;
        if(find(u) != find(v))
            hebing(u,v);
        add(u,v,w);
        MM = max(MM,w);
    }
    if(find(1) != find(n))
    {
        cout << -1 << endl;
        return 0;
    }
    int l = 0,r = MM;
    while(l < r)
    {
        int mid = (l + r) >> 1;
        if(check(mid))r = mid;
        else    l = mid + 1;
    }
    cout << l << endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/acioi/p/11715514.html