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 ...
随机推荐
- 超经典~超全的jQuery插件大全
海量的jQuery插件帖,很经典,不知道什么时候开始流传,很早以前就收藏过,为了工作方便还是发了一份放在日志里面. 其中有些已经无法访问,或许是文件移除,或许是被封锁.大家分享的东西,没什么特别的可说 ...
- python 将中文转拼音后填充到url做参数并写入excel
闲着没事写了个小工具,将中文转拼音后填充到url做参数并写如excel 一.先看下演示,是个什么东西 二.代码 代码用到一个中文转拼音的库,库是网上下的,稍微做了下修改,已经找不原来下载的地址了,然后 ...
- Discuz 取消 应用更新提醒 方法
管理员每次登录论坛,遇有后台没有更新的应用都会有应用更新提醒提醒,而且关了还会继续弹出,问题是有些应用原来我装了免费的,新版本推出来了是 要收费的,我不想要更新,或者是即使有免费的新版本了,而我只要使 ...
- day09-文件的操作
目录 文件的基本操作 文件 什么是文件 如何使用文件 打开&关闭文件 打开&关闭文件 del f和f.close()的区别 文件路径 打开模式(不写默认是r) 编码格式 补充(open ...
- 删除Git服务器文件但是保留本地文件
参考: https://blog.csdn.net/u012804886/article/details/83059315 https://www.cnblogs.com/wfsovereign/p/ ...
- ubuntu14.04禁用USB外存储设备
ubuntu 14.04中禁用usb外存储设备: 在网上找了很多方法,大概都是下面的命令,而实际测试的时候没有什么作用. gsettings set org.gnome.desktop.media-h ...
- Django - 模版语言循环字典
1.可以对传入字典参数做循环显示 views.py中代码: urls.py中代码: html中代码: 在模版语言中,可以对字典进行类似python中的操作(keys,values,items),需要注 ...
- Python基础-奇偶判断调用函数
编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数 1/1+1/3+...+1/n. 首先写一个n为偶数的函数: def peven(n): s = 0 ...
- C# 通俗说 内存的理解
一.概念 堆栈是什么? 在说堆栈之前,先说说内存是神马? 内存:程序在运行的过程,电脑需要不断通过CPU进行计算,这个计算的过程会读取并产生运算的数据,这些数据需要一个存储容器存放.这个容器,这就是内 ...
- h5知识总结
移动开发基本知识点一. 使用rem作为单位 html { font-size: 100px; } @media(min-width: 320px) { html { font-size: 100px; ...