POJ 1944:Fiber Communications
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 4236 | Accepted: 1276 |
Description
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
* Lines 2..P+1: two integers describing a pair of barns between which communication is desired. No pair is duplicated in the list.
Output
Sample Input
5 2
1 3
4 5
Sample Output
3
Hint
题意是给你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的更多相关文章
- [USACO2002][poj1944]Fiber Communications(枚举)
Fiber Communications Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3804 Accepted: 1 ...
- POJ1944 Fiber Communications (USACO 2002 February)
Fiber Communications 总时间限制: 1000ms 内存限制: 65536kB 描述 Farmer John wants to connect his N (1 <= N ...
- TOJ1550: Fiber Communications
1550: Fiber Communications Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal ...
- 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 ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- POJ 1944 - Fiber Communications
原题地址:http://poj.org/problem?id=1944 题目大意:有n个点排成一圈,可以连接任意两个相邻的点,给出 p 对点,要求这 p 对点必须直接或间接相连,求最少的连接边数 数据 ...
- POJ 1944 Fiber Communications (枚举 + 并查集 OR 线段树)
题意 在一个有N(1 ≤ N ≤ 1,000)个点环形图上有P(1 ≤ P ≤ 10,000)对点需要连接.连接只能连接环上相邻的点.问至少需要连接几条边. 思路 突破点在于最后的结果一定不是一个环! ...
- POJ 1027:The Same Game 较(chao)为(ji)复(ma)杂(fan)的模拟
The Same Game Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5168 Accepted: 1944 Des ...
- POJ 1459:Power Network(最大流)
http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...
随机推荐
- 引用类型--Function类型(函数声明与函数表达式、arguments.callee、caller、apply、call、bind)
在ECMAScript中函数实际上是对象.每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象的指针,不会与某个函数绑定 ...
- php循环语句for while do while的用法
循环结构 一.while循环 while(表达式){ 循环体;//反复执行,直到表达式为假} <?php$num = 1; while ($num <= 10){ print &qu ...
- PHP+swoole实现聊天群发功能
本篇文章主要介绍PHP+swoole实现聊天群发功能,感兴趣的朋友参考下,希望对大家有所帮助. php代码: $serv = new swoole_websocket_server("127 ...
- spark on yarn container分配极端倾斜
环境:CDH5.13.3 spark2.3 在提交任务之后,发现executor运行少量几台nodemanager,而其他nodemanager没有executor分配. 测试环境通过spark-s ...
- 题解 CF1131C 【Birthday】
CF大水题 题意:给你n个人,他们的身高是a[i],让你将这几个人排成一个环,使得他们两两之间身高差的和最小. 思路:简单到爆了,恶意评分上蓝.直接将那几个人排个序,然后按序左右放就行了,也就是说1号 ...
- 小程序转uni-app用到的一些方法
setData: function (obj) { let that = this; Object.keys(obj).forEach(function (key) { that.$set(that. ...
- P1067 试密码
P1067 试密码 转跳点:
- python 聚类分析 k均值算法
dataSet = [ #数据集 # 1 [0.697, 0.460], # 2 [0.774, 0.376], # 3 [0.634, 0.264], # 4 [0.608, 0.318], # 5 ...
- 打开c++ 项目遇到的错误
前言 后续持续更新: 无法打开源文件windows.h https://blog.csdn.net/Mr__George/article/details/87714252 找不到duilib.h ht ...
- Java连载79-Calendar解析
一. Calendar解析 package com.bjpowernode.java_learning; import java.util.Date; import java.text.ParseEx ...