日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

牛客假日团队赛8:F.Telephone Lines(二分+spfa)

發布時間:2024/9/3 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 牛客假日团队赛8:F.Telephone Lines(二分+spfa) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈接:https://ac.nowcoder.com/acm/contest/1069/F
來源:牛客網

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld
題目描述
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.
輸入描述:

  • 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 Li
    輸出描述:
  • 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.
    示例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

輸出
復制

4

說明
There are 5 poles. Pole 1 cannot be connected directly to poles 4 or 5. Pole 5 cannot be connected directly to poles 1 or 3. All other pairs can be connected. The phone company will provide one free cable.
If pole 1 is connected to pole 3, pole 3 to pole 2, and pole 2 to pole 5 then Farmer John requires cables of length 4, 3, and 9. The phone company will provide the cable of length 9, so the longest cable needed has length 4.
題意:
大概題意就是,給一些點之間建邊的花費,要使1與n連通,你建立的1–〉n這條路上,有k條邊的花費(任意k條)可以報銷(原題就是電力公司會免費提供),要你求解要自己出錢最大一條邊的權值最小是多少。

很明顯就是求解第k+1大最小。
轉換一下就是最多有k條邊比ans大
為什么是最多k條呢?
因為1–〉n這條路可能不足k條邊就可以使它相連(此時自己不用出錢)
ans一定是唯一的一個值
所以本題:用二分ans+spfa來做
spfa中dis數組中記錄的是1到各點有多少個點比二分的ans大
當dis[n]<=k說明此時二分的ans大了,二分ans要向前移動
AC_code:

#include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f const int MAXN = 1e4+5; struct Edge {int to;int val;Edge(int t,int v):to(t),val(v){} }; vector<Edge>vec[MAXN]; bool isIn[MAXN]; int dis[MAXN]; int n,p,k; bool spfa(int mid) {for(int i = 1; i<= n; i++){isIn[i] = false;dis[i] = INF;}dis[1] = 0;queue<int>q;isIn[1] = true;q.push(1);while(!q.empty()){int k = q.front();q.pop();isIn[k] = false;int iSize = vec[k].size();for(int i = 0; i < iSize; i++){int t = vec[k][i].to;int v = vec[k][i].val > mid;if(dis[t] > dis[k] + v){dis[t] = dis[k] + v;if(!isIn[t]){isIn[t] = true;q.push(t);}}}}return dis[n] <= k; } int main() {scanf("%d%d%d",&n,&p,&k);int x,y,v;for(int i = 0; i < p; i++){scanf("%d%d%d",&x,&y,&v);vec[x].push_back(Edge(y,v));vec[y].push_back(Edge(x,v));}int l = 0,r = 1e6;int res = -1;while(l <= r){int mid = (l+r)>>1;if(spfa(mid)){res = mid;r = mid - 1;}else{l = mid + 1;}}printf("%d\n",res);return 0; }

總結

以上是生活随笔為你收集整理的牛客假日团队赛8:F.Telephone Lines(二分+spfa)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。