博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2594 Treasure Exploration
阅读量:4310 次
发布时间:2019-06-06

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

Treasure Exploration
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 8879   Accepted: 3635

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

Source

,Li Haoyuan
 
【题解】
二分图可重最小路径覆盖裸题
先求传递闭包,然后连边
注意时间,数组尽量开小
1 #include 
2 #include
3 #include
4 #include
5 6 inline void read(int &x) 7 { 8 x = 0;char ch = getchar(), c = ch; 9 while(ch < '0' || ch > '9')c = ch, ch = getchar();10 while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();11 if(c == '-')x = -x;12 }13 14 const int INF = 0x3f3f3f3f;15 16 struct Edge17 {18 int u,v,next;19 Edge(int _u, int _v, int _next){u = _u, v = _v, next = _next;}20 Edge(){}21 }edge[250010];22 23 int head[510], cnt;24 25 inline void insert(int a, int b)26 {27 edge[++cnt] = Edge(a,b,head[a]);28 head[a] = cnt;29 }30 31 int n,m,lk[510],b[510],g[510][610];32 33 int dfs(int u)34 {35 for(register int pos = head[u];pos;pos = edge[pos].next)36 {37 int v = edge[pos].v;38 if(b[v])continue;39 b[v] = 1;40 if(lk[v] == -1 || dfs(lk[v]))41 {42 lk[v] = u;43 return 1;44 }45 }46 return 0;47 }48 49 int xiongyali()50 {51 int ans = 0;52 memset(lk, -1, sizeof(lk));53 for(register int i = 1;i <= n;++ i)54 {55 memset(b, 0, sizeof(b));56 ans += dfs(i);57 }58 return ans;59 }60 61 int main()62 {63 while(scanf("%d %d", &n, &m) != EOF && (n + m))64 {65 memset(edge, 0, sizeof(edge));66 memset(head, 0, sizeof(head));67 memset(g, 0, sizeof(g));68 cnt = 0;69 int tmp1, tmp2;70 for(register int i = 1;i <= m;++ i)71 {72 read(tmp1), read(tmp2);73 g[tmp1][tmp2] = 1;74 }75 for(register int k = 1;k <= n;++ k)76 for(int i = 1;i <= n;++ i)77 for(int j = 1;j <= n;++j)78 g[i][j] |= g[i][k] && g[k][j];79 for(register int i = 1;i <= n;++ i)80 for(int j = 1;j <= n;++ j)81 if(g[i][j]) insert(i, j);82 printf("%d\n", n - xiongyali());83 }84 return 0;85 }
POJ2594

 

转载于:https://www.cnblogs.com/huibixiaoxing/p/7585701.html

你可能感兴趣的文章
原!!mysql,几十万条数据中随机抽取1万以内的数据
查看>>
SQLMAP之tamper详解
查看>>
OpenCV-跟我学一起学数字图像处理之中值滤波
查看>>
使用cookie来做身份认证 转载https://www.cnblogs.com/sheldon-lou/p/9545726.html
查看>>
ASP.NET MVC学习系列(二)-WebAPI请求 转载https://www.cnblogs.com/babycool/p/3922738.html
查看>>
MemCache在.NET中使用Memcached.ClientLibrary详解 转发 https://www.cnblogs.com/li150dan/p/9529112.html...
查看>>
DB2查找替换字符串
查看>>
java可变参数
查看>>
SQLServer2008设置开启远程连接
查看>>
C#连接Sybase数据库,Anywhere 8
查看>>
CSS layout入门
查看>>
排序算法—冒泡排序
查看>>
Exchange邮件系统日志查看及管理
查看>>
匿名访问windows server 2008 R2 文件服务器的共享
查看>>
is_authenticate 和 login_required判断用户是否登录
查看>>
购物车
查看>>
java之线程池
查看>>
25 Android中dip、dp、sp、pt和px的区别
查看>>
繁星——JQuery选择器之层级
查看>>
邮件发送
查看>>