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

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

POJ 1944:Fiber Communications的更多相关文章

  1. [USACO2002][poj1944]Fiber Communications(枚举)

    Fiber Communications Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3804   Accepted: 1 ...

  2. POJ1944 Fiber Communications (USACO 2002 February)

    Fiber Communications 总时间限制:  1000ms 内存限制:  65536kB 描述 Farmer John wants to connect his N (1 <= N ...

  3. TOJ1550: Fiber Communications

    1550: Fiber Communications  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal ...

  4. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  5. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  6. POJ 1944 - Fiber Communications

    原题地址:http://poj.org/problem?id=1944 题目大意:有n个点排成一圈,可以连接任意两个相邻的点,给出 p 对点,要求这 p 对点必须直接或间接相连,求最少的连接边数 数据 ...

  7. POJ 1944 Fiber Communications (枚举 + 并查集 OR 线段树)

    题意 在一个有N(1 ≤ N ≤ 1,000)个点环形图上有P(1 ≤ P ≤ 10,000)对点需要连接.连接只能连接环上相邻的点.问至少需要连接几条边. 思路 突破点在于最后的结果一定不是一个环! ...

  8. POJ 1027:The Same Game 较(chao)为(ji)复(ma)杂(fan)的模拟

    The Same Game Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5168   Accepted: 1944 Des ...

  9. POJ 1459:Power Network(最大流)

    http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...

随机推荐

  1. Java的clone方法效率问题

    在Java中,经常会需要新建一个对象,很多情况下,需要这个新建的对象和现有的某个对象保持属性一致. 那么,就有两种方式来实现这个对象的构造: ①通过新建一个对象,为这个对象的属性根据原有对象的属性来进 ...

  2. Linux centosVMware shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量

    一. shell脚本介绍 shell是一种脚本语言 aming_linux blog.lishiming.net 可以使用逻辑判断.循环等语法 可以自定义函数 shell是系统命令的集合 shell脚 ...

  3. Hadoop的伪分布式安装和部署的流程

    1.准备工作 下载一些用到的命令 yum install -y vim yum install -y lrzsz yum install net-tools 目录约定 /opt #工作目录 /opt/ ...

  4. eclipse中maven web项目部署时缺少classes文件或者resources文件

    写这篇博客的原因 问题描述 昨天发现eclipse中maven web项目部署时缺少classes文件或者resources文件 本来以为是很常见的原因, 依次检查"Java Build P ...

  5. 玩玩负载均衡---在window与linux下配置nginx

      最近有些时间,开始接触负载均衡方面的东西,从硬件F5再到Citrix Netscalar.不过因为硬件的配置虽然不复杂,但昂贵的价格也让一般用户望而却步(十几万到几十万),所以只能转向nginx, ...

  6. HiBench成长笔记——(3) HiBench测试Spark

    很多内容之前的博客已经提过,这里不再赘述,详细内容参照本系列前面的博客:https://www.cnblogs.com/ratels/p/10970905.html 创建并修改配置文件conf/spa ...

  7. 记一次Redis+Getshell经验分享

    前言: 当我们接到一个授权渗透测试的时候,常规漏洞如注入.文件上传等尝试无果后,扫描端口可能会发现意外收获. 知己知彼乃百战不殆,Redis介绍: 简单来说 redis 就是一个Key-Value类型 ...

  8. R 误差自相关与DW检验

    R语言进行DW检验: library(lmtest) dw = dwtest(fm1) > dw Durbin-Watson test data: fm1 DW = 2.4994, p-valu ...

  9. Django null=True和blank=True的区别

    今天遇到一个问题: 在restframework框架中开发,数据库了创建了一个model的属性如下所示: remarks = models.CharField(verbose_name=u" ...

  10. SQL创建表格——手写代码

    打开phpstudy,打开Navicat for MySQL,进入要创建表格的数据库,点击上方“查询”按钮,“创建查询”,即可输入代码进行创建. 例: create table class( clas ...