POJ 1944:Fiber Communications

Fiber Communications

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 4236   Accepted: 1276

Description

Farmer John wants to connect his N (1 <= N <= 1,000) barns (numbered 1..N) with a new fiber-optic network. However, the barns are located in a circle around the edge of a large pond, so he can only connect pairs of adjacent barns. The circular configuration
means that barn N is adjacent to barn 1.

FJ doesn‘t need to connect all the barns, though, since only certain pairs of cows wish to communicate with each other. He wants to construct as few

connections as possible while still enabling all of these pairs to communicate through the network. Given the list of barns that wish to communicate with each other, determine the minimum number of lines that must be laid. To communicate from barn 1 to barn
3, lines must be laid from barn 1 to barn 2 and also from barn 2 to barn 3(or just from barn 3 to 1,if n=3).

Input

* Line 1: Two integers, N and P (the number of communication pairs, 1 <= P <= 10,000)

* Lines 2..P+1: two integers describing a pair of barns between which communication is desired. No pair is duplicated in the list.

Output

One line with a single integer which is the minimum number of direct connections FJ needs to make.

Sample Input

5 2
1 3
4 5

Sample Output

3

Hint

[Which connect barn pairs 1-2, 2-3, and 4-5.]

题意是给你1到N这么多的点,要求有P次连接,将P次连接中给的点连接起来,举例比如说1到3要连接有两种连接方法,1-2-3或者1-5-4-3。求满足所有连接中最少需要多少个线段连接。

真没想到暴力竟然还能n*n这么暴。。。看了别人的想法之后,觉得精彩的地方第一点在于如果i 到i+1这个线段是断了的话,而要求连接的线段又恰好在这之间,(即只有一种方法将之连接,逆过去连接),这种时候的做法是将to[1]=start,to[end]=N+1,分成两段来想,还避免了那个断了的影响,觉得这种想法真是赞啊!!!

还有一点是to数组的使用比我之前想象的简单多了,to[i]表示从i开始到to[i]已经连接上了,如果等于0直接pass掉,这样一个一个枚举也很简单方便。

还有一点是duandian表示当前能够到达的最远的点,这里的使用也很赞。

这三点真的值得我去好好研磨。

代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
using namespace std;

struct node
{
	int start;
	int end;

}node_one[10005];

bool cmp(node node1,node node2)
{
	if(node1.start == node2.start)
		return node1.end< node2.end;

	return node1.start<node2.start;
}

#define INF 0x3f3f3f3f

int N,P,i,j,Q1,Q2,Qmin,Qmax,ans,h;
int to[1005];

int main()
{
	cin>>N>>P;
	for(i=1;i<=P;i++)
	{
		cin>>Q1>>Q2;
		node_one[i].start = min(Q1,Q2);
		node_one[i].end = max(Q1,Q2);
	}
	sort(node_one+1,node_one+P+1,cmp);

	ans=INF;

	for(i=1;i<=N;i++)
	{
		memset(to,0,sizeof(to));
		for(j=1;j<=P;j++)
		{
			if(node_one[j].end>=i+1 && node_one[j].start<=i)
			{
				to[1]=max(to[1],node_one[j].start);
				to[node_one[j].end]=N+1;
			}
			else
			{
				to[node_one[j].start]=max(to[node_one[j].start],node_one[j].end);
			}
		}
		int duandian=0,result=0;
		for(j=1;j<=N;j++)
		{
			if(to[j]==0)continue;
			if(to[j]>duandian)
			{
				if(j>=duandian)
				{
					result+=(to[j]-j);
				}
				else
				{
					result+=(to[j]-duandian);
				}
				duandian=to[j];
			}
		}
		ans=min(ans,result);
	}
	cout<<ans<<endl;

	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-22 19:00:27

POJ 1944:Fiber Communications的相关文章

POJ 1963:All in All

All in All Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27707   Accepted: 11381 Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever

POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19941   Accepted: 6999 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1659:Frogs&#39; Neighborhood(Havel-Hakimi定理)

Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6898   Accepted: 3006   Special Judge Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居.现在已知每只青蛙的邻居数目x1, x2, ..

POJ 1422:Air Raid(最大独立集)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6547   Accepted: 3896 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an i

POJ 2965:The Pilots Brothers&#39; refrigerator

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18080   Accepted: 6855   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

POJ 1904:King&#39;s Quest【tarjan】

题目大意:给出一个二分图的完美匹配(王子和公主的烧死名单表),二分图x部和y部均只有n个点,问对于每一个x部的点,他能选择哪些点与之匹配 使得与之匹配后,剩余图的最大匹配仍然是n 思路:这题是大白书379页二分图的压轴题,在图论刷的题还不多时思考过这题,现在想来也不难想 这题引人瞩目的一点便是预先给出了一个二分图的初始匹配 对每个点枚举后增广显然不怎么可行,那么还是图论问题的经典思考方式,点和边各表示什么 题目的输入天然的给出了一个图,但对这题好像没什么用处,于是开始思考把给出的初始匹配的每条边

POJ 3083:Children of the Candy Corn(DFS+BFS)

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: 4039 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, ch

POJ 3014:Asteroids(二分匹配,匈牙利算法)

Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14399   Accepted: 7836 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K as

POJ 3692:Kindergarten(最大团)

Kindergarten Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4920   Accepted: 2399 Description In a kindergarten, there are a lot of kids. All girls of the kids know each other and all boys also know each other. In addition to that, some