博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1463 Strategic Game 匈牙利算法
阅读量:3904 次
发布时间:2019-05-23

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

题目:

Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?

Your program should find the minimum number of soldiers that Bob has to put for a given tree.
The input file contains several data sets in text format. Each data set represents a tree with the following description:
the number of nodes
the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifier
or
node_identifier:(0)
The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.
For example for the tree:
the solution is one soldier ( at the node 1).
The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following table:

Input

40:(1) 11:(2) 2 32:(0)3:(0)53:(3) 1 4 21:(1) 02:(0)0:(0)4:(0)

Output

12

Sample Input

40:(1) 11:(2) 2 32:(0)3:(0)53:(3) 1 4 21:(1) 02:(0)0:(0)4:(0)

Sample Output

12

思路:

这题感觉跟二分图的最大匹配数很像,因为二分图所组成的组数中的点可以通过一个点来统一管理。

套用了匈牙利算法,A了。

代码如下:

#include 
#include
#include
#include
using namespace std;const int maxn=1505;int head[maxn];int n;int match[maxn];int vis[maxn];struct edge{ int to; int next;}edge[maxn<<1];void add (int id,int u,int v){ edge[id].to=v; edge[id].next=head[u]; head[u]=id;}bool Find(int x){ for (int i=head[x];i!=-1;i=edge[i].next) { int u=edge[i].to; if(!vis[u]) { vis[u]=1; if(match[u]==-1||Find(match[u])) { match[u]=x; return true; } } } return false;}int algor(){ int sum=0; memset (match,-1,sizeof(match)); for (int i=0;i

 

转载地址:http://zzoen.baihongyu.com/

你可能感兴趣的文章
linux路由内核实现分析(三)---路由查找过程
查看>>
linux路由内核实现分析(四)---路由缓存机制
查看>>
并查集学习
查看>>
Linux内核中内存分配函数
查看>>
Linux守护进程的编程方法
查看>>
如何在LINUX中获取进程中某个虚拟地址所在物理内存地址
查看>>
inux内存管理之非连续物理地址分配(vmalloc)
查看>>
IP有效载荷压缩协议(IPComp)
查看>>
突破人生的瓶颈(心灵之灯)
查看>>
linux开机启动过程
查看>>
qt creator 文件移植到开发板上运行 的全过程
查看>>
linux中安装Qt 4.8.5
查看>>
linux中ls命令详解
查看>>
USB通信记事
查看>>
Android 编译(1)——Android编译步骤梳理
查看>>
编译器配置(1)——ARMv7,ARMv8(AArch64) 浮点配置等相关知识
查看>>
RK3399 OV13850摄像头配置
查看>>
Android 编译(2)——jack-server相关问题
查看>>
网络服务(2)——以太网配置IPV4和IPV6
查看>>
网络服务(3)——以太网phy的识别加载(RK3399)
查看>>