P1948 [USACO08JAN]Telephone Lines S(二分+spfa)

本文介绍了一种利用二分查找算法解决最小费用路径问题的方法。在给定的电话线杆网络中,需要找到从1号杆到N号杆的最短路径,同时电信公司提供一定数量的免费电缆。通过设置边权并运行最短路径算法,判断所需费用是否在二分查找的范围内,最终确定最小支出。这是一个典型的结合图论和搜索算法的实际应用问题。
摘要由CSDN通过智能技术生成

题目描述

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.

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

第i对电线杆的两个端点分别是 ai,bi​,它们的距离为 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

思路:

首先我们可以明确题要的是: 最大值的最小值

这恰是二分的标志!!!

 

如何二分 or 二分什么:

不难发现是要二分答案

如何写check函数(难点):

注:mid是二分中用于check的值

我们可以这样想:如果价格小于等于mid,那我们就把边权设置为0;

                             如果价格大于mid,那我们就把边权设置为1;

边权0 or 1代表什么:0 --> 不需要免费(不消耗k),1 --> 需要免费(消耗k)

跑最短路,如果结果 小于等于 k 则check 为 true;

                  如果结果 大于 k 则check 为 false;

 

AC代码如下:
 

#include <bits/stdc++.h>
#define buff                     \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0)
#define endl "\n"
using namespace std;
const int N = 20009;
int dist[N];
int ne[N], h[N], e[N], w[N], idx;
bool st[N];
int n, m, k;
void add(int a, int b, int c)
{
    idx++;
    e[idx] = b;
    ne[idx] = h[a];
    w[idx] = c;
    h[a] = idx;
}
bool spfa(int x)
{
    int val;
    for (int i = 1; i <= n; i++)
    {
        st[i] = 0;
        dist[i] = 0x3f3f3f3f;
    }
    queue<int> q;
    dist[1] = 0;
    st[1] = 1;
    q.push(1);
    while (q.size())
    {
        int t = q.front();
        q.pop();
        st[t] = 0;
        for (int i = h[t]; i; i = ne[i])
        {
            int j = e[i];

            if (w[i] - x > 0)
                val = 1;
            else
                val = 0;

            if (dist[j] > dist[t] + val)
            {
                dist[j] = dist[t] + val;
                if (!st[j])
                {
                    st[j] = 1;
                    q.push(j);
                }
            }
        }
    }
    if (dist[n] <= k)
        return 1;
    else
        return 0;
}
int main()
{
    buff;
    cin >> n >> m >> k;
    for (int i = 1; i <= m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
        add(b, a, c);
    }
    //二分答案
    int l = 0, r = 1000000;
    bool is_ok = 0;
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (spfa(mid))
        {
            r = mid;
            is_ok = 1;
        }
        else
            l = mid + 1;
    }
    if (is_ok)
        cout << l << endl;
    else
        cout << -1 << endl;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是P4087 [USACO17DEC]Milk Measurement的c++代码: ```c++ #include<bits/stdc++.h> using namespace std; int n,d,i,x,minn=1e9,maxn=-1e9,sum=7;//注意sum要初始化为7,因为一开始有三个人挤奶! map<int,int> mp; struct node{ int day,milk,id;//day表示某一天,milk表示这一天的产奶量,id表示这头牛的编号 }a[100010]; bool cmp(node x,node y){ return x.day<y.day; } int main(){ scanf("%d%d",&n,&d); for(i=1;i<=n;i++){ scanf("%d%d%d",&a[i].day,&a[i].id,&a[i].milk); minn=min(minn,a[i].id);//记录最小的牛的编号 maxn=max(maxn,a[i].id);//记录最大的牛的编号 } sort(a+1,a+n+1,cmp);//排序 for(i=1;i<=n;i++){ int p=a[i].id; mp[p]+=a[i].milk;//记录每头牛产奶总量 if(mp[p]-a[i].milk>=mp[minn]&&mp[p]>=mp[minn]){//如果这头牛的产奶总量减去这一天的产奶量后等于最小产奶量且这头牛的产奶总量大于等于最小产奶量 sum--; } if(mp[p]>=mp[maxn]&&mp[p]-a[i].milk<mp[maxn]){//如果这头牛的产奶总量大于等于最大产奶量且这头牛的产奶总量减去这一天的产奶量小于最大产奶量 sum++; } if(mp[p]-a[i].milk<mp[maxn]&&mp[p]>=mp[maxn]){//如果这头牛的产奶总量减去这一天的产奶量小于最大产奶量且这头牛的产奶总量大于等于最大产奶量 if(mp[maxn]-mp[p]+a[i].milk>0)sum++; } mp[p]-=a[i].milk;//减去这一天的产奶量 if(i==n||a[i].day!=a[i+1].day){//如果到了新的一天或者到了最后一天 if(mp[maxn]!=mp[a[i].id]&&mp[a[i].id]>=mp[maxn])sum++;//如果这头牛的产奶总量不等于最大产奶量且这头牛的产奶总量大于等于最大产奶量 if(mp[maxn]==mp[a[i].id]){//如果这头牛的产奶总量等于最大产奶量 if(a[i].id==maxn)sum+=0;//如果这头牛就是最大产奶量的牛,那么不需要增加计数器 else sum++;//否则需要增加计数器 } if(mp[minn]!=mp[a[i].id]&&mp[a[i].id]>=mp[minn])sum++;//如果这头牛的产奶总量不等于最小产奶量且这头牛的产奶总量大于等于最小产奶量 if(mp[minn]==mp[a[i].id]){ if(a[i].id==minn)sum+=0;//如果这头牛就是最小产奶量的牛,那么不需要增加计数器 else sum++;//否则需要增加计数器 } } } printf("%d\n",sum); return 0; } ``` 该题的解题思路是模拟,需要注意细节问题。我们可以首先将输入的数据按天数排序,然后模拟每一天挤奶的情况,并根据题目要求进行计数即可。具体细节请见代码注释。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Joanh_Lan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值