博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2594——最小路径覆盖
阅读量:4974 次
发布时间:2019-06-12

本文共 3672 字,大约阅读时间需要 12 分钟。

Description

Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored treasure? If you never have such experiences, you would never know what fun treasure exploring brings to you. 
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

The input will consist of several test cases. For each test case, two integers N (1 <= N <= 500) and M (0 <= M <= 5000) are given in the first line, indicating the number of points and the number of one-way roads in the graph respectively. Each of the following M lines contains two different integers A and B, indicating there is a one-way from A to B (0 < A, B <= N). The input is terminated by a single line with two zeros.

Output

For each test of the input, print a line containing the least robots needed.

Sample Input

1 02 11 22 00 0

Sample Output

112 最小路径覆盖:一个有向图(无向图也可),每次可以从任意一个点出发访问一条链,求覆盖整个图所用的最少次数。 建立匹配:将每个点 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
#include
#include
#include
#include
#include
#include
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;}

  

 

转载于:https://www.cnblogs.com/lzhAFO/p/8013200.html

你可能感兴趣的文章
C# 小叙 Encoding (二)
查看>>
python创建对象数组避免浅拷贝
查看>>
CSS自学笔记(14):CSS3动画效果
查看>>
项目应用1
查看>>
Ubuntu下配置jdk和tomcat
查看>>
大型网站的演变升级
查看>>
图片延迟加载的实现
查看>>
C# 委托链(多播委托)
查看>>
解密个推持续集成
查看>>
基本SCTP套接字编程常用函数
查看>>
C 编译程序步骤
查看>>
页面抓取匹配时,万恶的\r,\n,\t 要先替换掉为空,出现匹配有问题,都是这个引起的...
查看>>
利用Node.js调用Elasticsearch
查看>>
构造函数
查看>>
LeetCode N-Queens
查看>>
jstat 命令
查看>>
leetcode[155]Min Stack
查看>>
《代码不朽:编写可维护软件的10大要则(C#版)》读后感
查看>>
04、我的.net Core的学习 - 网页版Hello World
查看>>
分块学习
查看>>