USACO08JAN Telephone Lines 题解

1.USACO08JAN  Telephone Lines 题面

由于问的是最大值最小,所以二分加验证就好了

比较显然的,题干问的是第k+1长的路最短;

那么二分答案是正确的方向;

但是怎么验证?

我们可以将所有边权大于二分的答案的边视为边权是1,否则看成0;

然后从1~n跑最短路,如果答案大于二分的答案那么就不成立,否则成立;

这种思维比较重要,代码还是很简单的;

#include <bits/stdc++.h>
using namespace std;
int head[2000010],cnt;
class littlestar{
	public:
		int to;
		int nxt;
		int w;
		void add(int u,int v,int gg){
			nxt=head[u];
			to=v;
			w=gg;
			head[u]=cnt;
		}
}star[2000010];
int n,p,k;
int dis[100010],vis[100010];
bool SPFA(int x)
{
	queue<int> q;
	memset(dis,0x3f,sizeof(dis));
	dis[1]=0;
	memset(vis,0,sizeof(vis));
	q.push(1);
	while(q.size()){
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=head[u];i;i=star[i].nxt){
			int v=star[i].to;
			if(dis[v]>dis[u]+(star[i].w>x)){
				dis[v]=dis[u]+(star[i].w>x);
				if(!vis[v]){
					vis[v]=1;
					q.push(v);
				}
			}
		}
	}
	if(dis[n]<=k) return 1;
	else{
		return 0;
	}
}
int main()
{
	cin>>n>>p>>k;
	for(int i=1;i<=p;i++){
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		star[++cnt].add(u,v,w);
		star[++cnt].add(v,u,w);
	}
	int l=0,r=1000000;
	while(l<r){
		int mid=(l+r)/2;
		if(SPFA(mid)){
			r=mid;
		}
		else{
			l=mid+1;
		}
	}
	if(l==1000000) l=-1;
	cout<<l<<endl;
}

2.[JLOI2011]飞行路线题面

这道题很上一道题不太一样,所求的是对于每种方案去掉前k大边后的最短路;

首先由于k很小,可以进行分层图;

第i层图表示目前已经删去了i条边的最短路;

对于点对(i,j)在任意层中边权是w;从第i层到第i+1层的边权是0;

然后堆优化+dijkstra就可以了;

#include <bits/stdc++.h>
using namespace std;
int head[5000010],cnt;
class littlestar{
	public:
		int to;
		int nxt;
		int w;
		void add(int u,int v,int gg){
			nxt=head[u];
			to=v;
			w=gg;
			head[u]=cnt;
		}
}star[5000010];
int n,p,k;
int dis[5000010],vis[5000010];
int S,T;
void dijkstra()
{
	memset(dis,0x3f,sizeof(dis));
	priority_queue<pair<int,int> > q;
	q.push(make_pair(0,S));
	dis[S]=0;
	while(q.size()){
		int u=q.top().second;
		q.pop();
		if(vis[u]) continue;
		vis[u]=1;
		for(int i=head[u];i;i=star[i].nxt){
			int v=star[i].to;
			if(dis[v]>dis[u]+star[i].w){
				dis[v]=dis[u]+star[i].w;
				q.push(make_pair(-dis[v],v));
			}
		}
	}
}
int main()
{
	cin>>n>>p>>k;
	cin>>S>>T;
	++S;++T;
	for(int i=1;i<=p;i++){
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		++u; ++v;
		star[++cnt].add(u,v,w);
		star[++cnt].add(v,u,w);
		for(int j=0;j<k;j++){
			star[++cnt].add((j+1)*n+u,(j+1)*n+v,w);
			star[++cnt].add((j+1)*n+v,(j+1)*n+u,w);
			star[++cnt].add(j*n+u,(j+1)*n+v,0);
			star[++cnt].add(j*n+v,(j+1)*n+u,0);
		}
	}
	for(int j=0;j<=k;j++){
		star[++cnt].add(j*n+T,n*30+T,0);
	}
	dijkstra();
	cout<<dis[n*30+T];
}

3.[BJWC2012]冻结

和上一道题思路一样,都是分层图,但连接第i层和第i+1层的边权不再是0,而是w/2;

#include <bits/stdc++.h>
using namespace std;
int head[5000010],cnt;
class littlestar{
	public:
		int to;
		int nxt;
		int w;
		void add(int u,int v,int gg){
			nxt=head[u];
			to=v;
			w=gg;
			head[u]=cnt;
		}
}star[5000010];
int n,p,k;
int dis[5000010],vis[5000010];
void dijkstra()
{
	memset(dis,0x3f,sizeof(dis));
	priority_queue<pair<int,int> > q;
	q.push(make_pair(0,1));
	dis[1]=0;
	while(q.size()){
		int u=q.top().second;
		q.pop();
		if(vis[u]) continue;
		vis[u]=1;
		for(int i=head[u];i;i=star[i].nxt){
			int v=star[i].to;
			if(dis[v]>dis[u]+star[i].w){
				dis[v]=dis[u]+star[i].w;
				q.push(make_pair(-dis[v],v));
			}
		}
	}
}
int main()
{
	cin>>n>>p>>k;
	for(int i=1;i<=p;i++){
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		star[++cnt].add(u,v,w);
		star[++cnt].add(v,u,w);
		for(int j=0;j<k;j++){
			star[++cnt].add((j+1)*n+u,(j+1)*n+v,w);
			star[++cnt].add((j+1)*n+v,(j+1)*n+u,w);
			star[++cnt].add(j*n+u,(j+1)*n+v,w/2);
			star[++cnt].add(j*n+v,(j+1)*n+u,w/2);
		}
	}
	for(int j=0;j<=k;j++){
		star[++cnt].add(j*n+n,n*30+1,0);
	}
	dijkstra();
	cout<<dis[n*30+1];
}

4.[USACO09FEB]改造路Revamping Trails

思路重复,代码几乎一样,就不多说了;把这到题放到这里的原因是想告诉大家,USACO的题要好好做啊,很多省选题都来源于此;

#include <bits/stdc++.h>
using namespace std;
int head[5000010],cnt;
class littlestar{
	public:
		int to;
		int nxt;
		int w;
		void add(int u,int v,int gg){
			nxt=head[u];
			to=v;
			w=gg;
			head[u]=cnt;
		}
}star[5000010];
int n,p,k;
int dis[5000010],vis[5000010];
void dijkstra()
{
	memset(dis,0x3f,sizeof(dis));
	priority_queue<pair<int,int> > q;
	q.push(make_pair(0,1));
	dis[1]=0;
	while(q.size()){
		int u=q.top().second;
		q.pop();
		if(vis[u]) continue;
		vis[u]=1;
		for(int i=head[u];i;i=star[i].nxt){
			int v=star[i].to;
			if(dis[v]>dis[u]+star[i].w){
				dis[v]=dis[u]+star[i].w;
				q.push(make_pair(-dis[v],v));
			}
		}
	}
}
int main()
{
	cin>>n>>p>>k;
	for(int i=1;i<=p;i++){
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		star[++cnt].add(u,v,w);
		star[++cnt].add(v,u,w);
		for(int j=0;j<k;j++){
			star[++cnt].add((j+1)*n+u,(j+1)*n+v,w);
			star[++cnt].add((j+1)*n+v,(j+1)*n+u,w);
			star[++cnt].add(j*n+u,(j+1)*n+v,0);
			star[++cnt].add(j*n+v,(j+1)*n+u,0);
		}
	}
	for(int j=0;j<=k;j++){
		star[++cnt].add(j*n+n,n*30+1,0);
	}
	dijkstra();
	cout<<dis[n*30+1];
}

原文地址:https://www.cnblogs.com/kamimxr/p/11650105.html

时间: 2024-10-28 22:51:58

USACO08JAN Telephone Lines 题解的相关文章

洛谷 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 a

P1948 [USACO08JAN]电话线Telephone Lines

P1948 [USACO08JAN]电话线Telephone Lines 题目描述 多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人.该市周围分布着N(1<=N<=1000)根据1--n顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有p(1<=p<=10000)对电话杆可以拉电话线.其他的由于地震使得无法连接. 第i对电线杆的两个端点分别是ai,bi,它们的距离为li(1<=li<=1000000)

bzoj1614[Usaco2007 Jan]Telephone Lines架设电话线*

bzoj1614[Usaco2007 Jan]Telephone Lines架设电话线 题意: n个节点,1号节点已经连入互联网,现在需要将整个图连入网络.有K条边可以免费连接,最后总费用为所有连边费用的最大值,求最小总费用.n≤10000 题解: 二分费用,将连边费用大于二分值的长度记为1,否则记为0,求最短路,如果到某个点的距离超过k,则需要增加答案,继续二分. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include &l

[bzoj1614][Usaco2007Jan]Telephone Lines 架设电话线_二分答案_最短路

Telephone Lines bzoj-1614 Usaco-2007Jan 题目大意:给你一个n个点m条边的带边权无向图,求最短路.可以选取k条边免费. 注释:$1\le n\le 10^3$,$1\le m\le 10^5$ 想法:一眼分层图最短路啊! 我都想出来了就上网查一下题解吧 卧槽??二分+spfa?? 其实这个题不用分层图 我们二分答案,二分出最大值,然后将图中所有比mid大的边设为1,小的设为0,然后跑最短路.如果最短路的值比k大,说明最大值一定比二分的mid大.因为我的最短路

[BZOJ] 1614: [Usaco2007 Jan]Telephone Lines架设电话线

1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1806  Solved: 773[Submit][Status][Discuss] Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1,000)根按1..N顺次编号的废

poj 3662 Telephone Lines dijkstra+二分搜索

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accepted: 2071 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

POJ 3662 Telephone Lines (分层图)

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6785   Accepted: 2498 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

hust 1039 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 telep

POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 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