POJ 3662 [USACO08JAN]電話線Telephone Lines SPFA+二分答案
title
Time Limit: 1000MS | Memory Limit: 65536K |
---|---|
Total Submissions: 9674 | Accepted: 3483 |
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 {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 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 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6
Sample Output
4
Source
USACO 2008 January Silver
code
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
template<typename T>inline void read(T &x)
{
x=0;
T f=1,ch=getchar();
while (!isdigit(ch) && ch^'-') ch=getchar();
if (ch=='-') f=-1, ch=getchar();
while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
x*=f;
}
int ver[maxn],edge[maxn],Next[maxn],len[maxn],head[maxn],tot;
inline void add(int x,int y,int z)
{
ver[++tot]=y,len[tot]=z,Next[tot]=head[x],head[x]=tot;
}
int n,p,k;
int dist[maxn],vis[maxn];
inline int spfa(int s)
{
memset(dist,0x3f,sizeof(dist));
memset(vis,0,sizeof(vis));
queue<int>q;
dist[s]=0,vis[s]=1;
q.push(s);
while (!q.empty())
{
int x=q.front();
q.pop();
vis[x]=0;
for (int i=head[x];i;i=Next[i])
{
int y=ver[i],z=edge[i];
if (dist[y]>dist[x]+z)
{
dist[y]=dist[x]+z;
if (!vis[y]) q.push(y),vis[y]=1;
}
}
}
return dist[n]<=k;
}
inline int check(int mid)
{
for (int x=1;x<=n;++x)
for (int i=head[x];i;i=Next[i])
if (len[i]<=mid) edge[i]=0;
else edge[i]=1;
return spfa(1);
}
int main()
{
int r=-1,ans=-1;
read(n);read(p);read(k);
for (int i=1;i<=p;++i)
{
int x,y,z;
read(x);read(y);read(z);
add(x,y,z);add(y,x,z);
r=max(r,z);
}
int l=0;
while (l<=r)
{
int mid=(l+r)>>1;
if (check(mid)) r=mid-1,ans=mid;
else l=mid+1;
}
printf("%d\n",ans);
return 0;
}
所謂的雙端佇列優化,吸了O2才過,也許是我程式碼能力太菜了。
評測結果
// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
template<typename T>inline void read(T &x)
{
x=0;
T f=1,ch=getchar();
while (!isdigit(ch) && ch^'-') ch=getchar();
if (ch=='-') f=-1, ch=getchar();
while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
x*=f;
}
int ver[maxn],edge[maxn],Next[maxn],head[maxn],tot;
inline void add(int x,int y,int z)
{
ver[++tot]=y,edge[tot]=z,Next[tot]=head[x],head[x]=tot;
}
int n,p,k,maxmum;
int dist[maxn],vis[maxn];
inline int bfs(int mid)//bfs求最短路
{
memset(dist,0,sizeof(dist));
memset(vis,0,sizeof(vis));
deque<int>q;
dist[1]=0,vis[1]=1;
q.push_back(1);
while (!q.empty())
{
int x=q.front();
q.pop_front();
for (int i=head[x];i;i=Next[i])
{
int y=ver[i];
if (!vis[y] || dist[y]>=dist[x]+1)
{
if (edge[i]<=mid)//將小於等於mid的邊看做權值為零的邊
{
vis[y]=1,q.push_front(y);
dist[y]=dist[x];
}
else//然後將大於mid的邊看做權值為1的邊
{
vis[y]=1,q.push_front(y);
dist[y]=dist[x]+1;
}
}
}
}
if (dist[n]>k) return 0;//若最短路的長度大於k,則要連線的對數大於k對,在[mid+1,r]中繼續二份查詢
else return 1;//若最短路的長度小於k,則要連線的對數比k小,在[l,mid]中繼續二份查詢
}
int main()
{
read(n);read(p);read(k);
while (p--)
{
int x,y,z;
read(x);read(y);read(z);
add(x,y,z);add(y,x,z);
maxmum=max(maxmum,z);
}
int l=1,r=maxmum;
while (l<r)//可以二分最大的花費mid,mid屬於[l,r](l為最小花費,r為最大花費)
{
int mid=(l+r)>>1;
if (bfs(mid)) r=mid;
else l=mid+1;
}
if (l!=1) printf("%d\n",l);//最終,l即為所求
else puts("-1");
return 0;
}
相關文章
- Telephone Lines S
- POJ 2749 二分 + 2-SAT
- POJ - 3041 Asteroids 【二分圖匹配】AST
- 二分答案法
- kuangbin——Hamburgers(二分答案)
- c++ 二分答案C++
- 二分答案解題技巧
- POJ-3061 Subsequence(字首和+二分/尺取)
- POJ1743 Musical Theme(字尾陣列 二分)陣列
- 關押罪犯(二分答案+染色法判二分圖)
- POJ 3014:Asteroids(二分匹配,匈牙利演算法)AST演算法
- POJ 3667 Hotel 線段樹
- poj 2667 hotel 線段樹
- 【TJOI2016】【bzoj4552】排序(二分答案+線段樹01排序)排序
- 【二分答案】P2390 地標訪問
- P1852 跳跳棋 [LCA思想+二分答案]
- POJ 3233 Matrix Power Series (矩陣快速冪+等比數列二分求和)矩陣
- P1353 [USACO08JAN] Running S
- 新百勝線上電話新百勝線上開戶
- 騰龍公司現場火爆線上電話19143200444
- 銀鑽國際娛樂電話熱線18487970283
- 洛谷P4632 [APIO2018] New Home 新家(動態開節點線段樹 二分答案 掃描線 set)API
- C240817C. 團隊協作:二分答案+貪心
- 【題解】AGC007E | 二分答案 複雜度分析GC複雜度
- Gym 101962I Colonial Mansions(二分答案 + 資料結構)資料結構
- 新 金 寶 電 話 熱 線 17176934555放間奧被禁
- 新 金 寶 電 話 熱 線 17176934555那地方花篩
- 騰龍線上真人實體點選電話17608834388
- 將 VoIP 電話直接連線到 Asterisk 伺服器AST伺服器
- 打電話
- 聯賽模擬測試5 平均數 二分答案+逆序對
- How do I reverse selected lines order in Vim?
- POJ 2777 Count Color (線段樹+狀態壓縮)
- 新 金 寶 電 話 熱 線 17176934555偶好你哦你
- 百盛帝寶聯絡熱線電話184 8797 0283
- 電話號格式
- poj 2031
- poj 3461