POJ 3662 [USACO08JAN]電話線Telephone Lines SPFA+二分答案

~hsm~發表於2019-02-24

title

POJ 3662
LUOGU 1948

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;
}

相關文章