poj2594——最小路径覆盖
Description
Recently, a company named EUC (Exploring the Unknown Company) plan to explore an unknown place on Mars, which is considered full of treasure. For fast development of technology and bad environment for human beings, EUC sends some robots to explore the treasure.
To make it easy, we use a graph, which is formed by N points (these N points are numbered from 1 to N), to represent the places to be explored. And some points are connected by one-way road, which means that, through the road, a robot can only move from one end to the other end, but cannot move back. For some unknown reasons, there is no circle in this graph. The robots can be sent to any point from Earth by rockets. After landing, the robot can visit some points through the roads, and it can choose some points, which are on its roads, to explore. You should notice that the roads of two different robots may contain some same point.
For financial reason, EUC wants to use minimal number of robots to explore all the points on Mars.
As an ICPCer, who has excellent programming skill, can your help EUC?
Input
Output
Sample Input
1 0
2 1
1 2
2 0
0 0
Sample Output
1
1
2 最小路径覆盖:一个有向图(无向图也可),每次可以从任意一个点出发访问一条链,求覆盖整个图所用的最少次数。
建立匹配:将每个点 i 拆为 i 和 i',如果在原图中 i -> j 有边(有向边),那么在新图中连边 i -> j'
ans=边集大小V-匹配数
理解:首先决定用V次访问,每次访问都只访问一个点来完成覆盖任务。若i和j匹配,则认为到i的路径下一步经过j,那么每一个匹配都会减少一次访问。 本题还有些特殊,每个点都可以重复经过。
处理方法:认为一条链上i->j->k->p->q,i可以飞到k、p、q,j可以飞到p、q,其他同理
交了两遍,第一遍板子写错了呜呜呜
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
int read(){
int xx=0,ff=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
return xx*ff;
}
int N,M,lin[1010],len;
struct edge{
int y,next;
}e[250100];
bool f[510][510];
inline void insert(int xx,int yy){
e[++len].next=lin[xx];
lin[xx]=len;
e[len].y=yy;
}
void floyd(){
for(int i=1;i<=N;i++)
f[i][i]=1;
for(int k=1;k<=N;k++)
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
f[i][j]|=(f[i][k]&f[k][j]);
len=0;
memset(lin,0,sizeof(lin));
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
if(i!=j&&f[i][j])
insert(i,j+N);
}
int match[1010],vis[1010],tim,pretim,ans;
bool hun(int x){
for(int i=lin[x];i;i=e[i].next){
if(vis[e[i].y]<=pretim){
vis[e[i].y]=++tim;
if(match[e[i].y]==0||hun(match[e[i].y])){
match[x]=e[i].y;
match[e[i].y]=x;
return 1;
}
}
}
return 0;
}
int main(){
//freopen("in","r",stdin);
//freopen("out","w",stdout);
while(1){
N=read(),M=read();
if((!N)&&(!M))
break;
memset(f,0,sizeof(f));
for(int i=1;i<=M;i++){
int t1=read();
int t2=read();
f[t1][t2]=1;
}
floyd();
memset(match,0,sizeof(match));
memset(vis,0,sizeof(vis));
tim=pretim=ans=0;
for(int i=1;i<=N;i++)
if(!match[i]){
pretim=tim;
vis[i]=++tim;
if(hun(i))
ans++;
}
printf("%d\n",N-ans);
}
return 0;
}
poj2594——最小路径覆盖的更多相关文章
- poj2594最小路径覆盖+floyd
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8909 Accepted: 3 ...
- POJ2594 最小路径覆盖
题意: 题意就是给你个有向无环图,问你最少放多少个机器人能把图全部遍历,机器人不能走回头路线. 思路: 如果直接建图,跑一遍二分匹配输出n - 最大匹配数会跪,原因是这个题目和以 ...
- POJ2594 Treasure Exploratio —— 最小路径覆盖 + 传递闭包
题目链接:https://vjudge.net/problem/POJ-2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 655 ...
- poj2594 (最小路径覆盖 + floyd)
题目链接 http://poj.org/problem?id=2594) 题目大意: 一个有向图中, 有若干条连接的路线, 问最少放多少个机器人,可以将整个图上的点都走过. 最小路径覆盖问题. 分析 ...
- poj2594 机器人寻找宝藏(最小路径覆盖)
题目来源:http://poj.org/problem?id=2594 参考博客:http://www.cnblogs.com/ka200812/archive/2011/07/31/2122641. ...
- POJ2594 Treasure Exploration(最小路径覆盖)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8550 Accepted: 3 ...
- POJ2594:Treasure Exploration(Floyd + 最小路径覆盖)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 9794 Accepted: 3 ...
- POJ-2594 Treasure Exploration,floyd+最小路径覆盖!
Treasure Exploration 复见此题,时隔久远,已忘,悲矣! 题意:用最少的机器人沿单向边走完( ...
- POJ-2594 Treasure Exploration floyd传递闭包+最小路径覆盖,nice!
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8130 Accepted: 3 ...
随机推荐
- Robomongo 0.9.0 连接mongo数据库时,提示连接失败 的解决方案
Robomongo 0.9.0 连接mongo数据库时,提示连接失败.(IP和端口号确定是对的) 基本注意点: 1.mongodb服务打开,打开时,指定端口号,默认为27017,使用默认值,则不用指定 ...
- Linq处理decimal字段汇总Sum()为NULL
xxxxxxxx.Sum(f => f.jifen).GetValueOrDefault(0)
- js 判断 微信浏览器
<script type="text/javascript"> window.onload = function() { isWeixinBrowser(); } // ...
- C#斐波那契数列方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Labview学习笔记(三)
一.数据 1.数值控件 (1)数值控件 根据不同的模拟状态,放置不同控件 (2)显示格式 为了程序显示,需要设置数值型控件的表示法.数值范围.显示格式等属性. 一般来说,长度越长,则可以表示的数值范围 ...
- sysbench_memory
对于内存而言,这里--memory-total-size=100G 就是 意味着 total number of events: 104857600 1. --memory-total-size=10 ...
- Jenkins+Ant+Jmeter自动化集成测试实例
通过学习Jmeter自动化测试,接触到了Ant命令和其构建文件build.xml文件的编写,与此同时,通过将测试项目集成在jenkins上,进一步学习了jenkins的一些环境配置知识.以下是自己的初 ...
- UVA1395 Slim Span(kruskal)
题目:Slim Span UVA 1395 题意:给出一副无向有权图,求生成树中最小的苗条度(最大权值减最小权值),如果不能生成树,就输出-1: 思路:将所有的边按权值有小到大排序,然后枚举每一条边, ...
- 爬虫文件存储:txt文档,json文件,csv文件
5.1 文件存储 文件存储形式可以是多种多样的,比如可以保存成 TXT 纯文本形式,也可以保存为 Json 格式.CSV 格式等,本节我们来了解下文本文件的存储方式. 5.1.1 TXT文本存储 将数 ...
- axios的基本概念和安装以及配置方法
ajax:异步请求,是一种无需再重新加载整个网页的情况下,能够更新部分网页的技术 axios:用于浏览器和node.js的基于promise的HTTP客户端 1.从浏览器制作XMLHttpReques ...