title: woj1005-holding animals-01pack

date: 2020-03-05

categories: acm

tags: [acm,woj,pack]

01背包。中等题。

description

The total available floor space on the ark would have been over 100,000 square feet, which would be more floor space than in 20 standard sized

basketball courts. But in our story, we assume that the land dwelling air breathing animals is so many that the Ark can't contain all of them.

We know that the size of each kind of animal is different and the God has his own favorite. The God assigns each kind of animal a point to

show his favorite, and then lists points of every kind of animal to Noah. Noah must let some kinds of animals into the Ark and refuse

the others to maximize the total points. Noah cries for he doesn't know computer programming.

It 's an easy problem for you,right? So,rescue Noah!

输入格式

There will be multiple test cases. For each test case,the first line contains an integer n(n<=100) representing the number of species of the land

dwelling air breathing animals all over the world.

In the next n lines,there will be two integers in each line,separated by a single space,where the first integer shows the size of a kind of animal

and the second integer shows the point. The size and the point for all the animals will not exceed 10000.

The last line contains an integer s(s <= 100,000) indicating the size of Noah's Ark.

输出格式

For each test case,you should output a line that contains a single integer to describe the maximal point Noah can get.

样例输入

2

10 20

20 30

30

3

10 20

30 30

20 20

30

样例输出

50

40

code

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std; const int maxn=10005;
int value[maxn];
int sizee[maxn];
int final[100005]; // memset(final,0,sizeof(int)*4); 注意memset是字节为单位,只能赋值0。比如int 4字节0000 0X01010101.
//速度慢 另外注意一个字节2个16进制数
// memset(a,0x3f,sizeof(a)) int main(){
int n=0,noah=0;
while(cin>>n){
for(int i=0;i<n;i++){
cin>>sizee[i]>>value[i];
}
cin>>noah;
for(int i=0;i<=noah;i++) final[i]=0;
for(int i=0;i<n;i++)
for(int j=noah;j>0;j--) //倒序理解就是01背包只有一件,前i-1件已经判断完了,只判断这一件,在比较小的情况下放不影响在比较大的情况放
if(j>=sizee[i]) //final可行因为如果小的能放大的也能放。
final[j]=max(final[j],final[j-sizee[i]]+value[i]);
cout<<final[noah]<<endl;
}
return 0;
}

title: woj1006-Language of animals-BFS

date: 2020-03-05

categories: acm

tags: [acm,woj,bfs]

中等题。

注意数据很大,边没有权,就BFS,邻接表。

description

According to Ernest Mayr, America's leading taxonomist, there are over 1 million species of animals in the world.When all

kinds of animals took boat,they found a problem.Each kind of animal had its own language,so the communication became

very hard.Assume that animal A could communicate with animal B but it couldn't communicate with animal C,while animal C

could only communicate with animal B. So,animal B must translate what one said to the other if animal A and animal C wanted

to communicate with each other.We called animal B translator.Noah wanted to know the least translators if two animals

wanted to communicate with each other.

输入格式

There will be only one test case.The first line contains two integers n(2 <= n <=200,000) and

m(1 <= m <= 300,000),where n represents the number of animals and m represents the number of pairs which could

communicate with each other. The next m lines contains two numbers each,representing two animals who can communicate

with each other. The number starts at 0.The next line contains a integer k(k<=20) representing the number of queries,and

the last k lines contains two number each,representing two animals who wanted to communicate with each other.

输出格式

For each query,you should output the least translators if two animals in that query wanted to communicate with each other.

If they couldn't communicate with each other by translating ,output "-1".

样例输入

3 2

0 1

1 2

2

0 0

0 2

样例输出

0

1

code

//数据范围很大,不能用邻接矩阵
//没有边权,BFS就行,不用最短路 // 建图用链表或者vector
// BFS用QUEUE DFS用栈
#include <iostream>
#include<cstdio>
#include<vector>
#include<queue> using namespace std; const int maxn= 200005; //我傻了 写成20000+5,然后SEGMENTFAULT vector<int> mapp[maxn];
bool visited[maxn]; struct Node {
int a, len; //s 到 a的距离 len
Node(int a, int len) : a(a), len(len) {}
}; queue<Node> que; void clearque(queue<Node>& q){
queue<Node> empty;
swap(empty, q);
} int bfs(int s, int e)
{
for(int i=0;i<maxn;i++)
visited[i]=false;
visited[s] = true;
clearque(que);
que.push(Node(s, 0));
while(!que.empty()) {
Node tmp = que.front();
que.pop();
if(tmp.a == e)
return tmp.len==0?0:tmp.len-1;
// if(!mapp[tmp.a].empty())
for(vector<int>::iterator iter = mapp[tmp.a].begin(); iter != mapp[tmp.a].end(); iter++) {
if(!visited[*iter]) {
visited[*iter] = true;
que.push(Node(*iter, tmp.len + 1));
}
}
}
return -1;
} int main()
{
int n=0,m=0,q=0,x=0,y=0;
while(scanf("%d%d", &n, &m)==2) {
for(int i=0;i<n;i++)
mapp[i].clear();
for(int i=0;i<m;i++){
scanf("%d%d", &x, &y); mapp[x].push_back(y); mapp[y].push_back(x);
}
scanf("%d", &q);
for(int i=0;i<q;i++){
scanf("%d%d", &x, &y);
cout<<bfs(x,y)<<endl;
}
}
return 0;
}

woj1005-holding animals-01pack woj1006-Language of animals-BFS的更多相关文章

  1. 【256】◀▶IEW-答案

    附答案 Unit I Fast food Model Answers: Model 1 The pie chart shows the fast foods that teenagers prefer ...

  2. 【255】◀▶IEW-Unit20

    Unit 20 Environment: Tourism I.定语从句及分词在雅思写作中的运用 定语从句: 1. 先行词 2. 关系词:关系代词.关系副词 3. 非限制性定语从句 4. 分词和定语从句 ...

  3. Unity 最佳实践

    转帖:http://www.glenstevens.ca/unity3d-best-practices/ 另外可以参考:http://devmag.org.za/2012/07/12/50-tips- ...

  4. FreeMarker中文API手册(完整)

    FreeMarker概述 FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写 FreeMarker被设计用来生成HTML Web页面,特别是基于MVC模式的应用 ...

  5. PHP 7 测试用例(转)

    性能改善:PHP 7高达两倍快的PHP 5.6 显著减少内存使用 抽象语法树 一致的64位支持 改进的异常层次结构 许多转化为异常致命错误 安全随机数发生器 删除旧的和不支持的SAPIs和扩展 空合并 ...

  6. .NET软件工程师面试总结

    1.手写画出系统架构图,系统代码架构,有什么技术难点?  2.手写画出系统部署图 CDN(一般购买别人的服务器会自动CDN,他们自己配置就OK啦) 3.asp.net 的session怎么实现会话共享 ...

  7. 好RESTful API的设计原则

    说在前面,这篇文章是无意中发现的,因为感觉写的很好,所以翻译了一下.由于英文水平有限,难免有出错的地方,请看官理解一下.翻译和校正文章花了我大约2周的业余时间,如有人愿意转载请注明出处,谢谢^_^ P ...

  8. 【ruby】ruby基础知识

    Install Ruby(安装) For windows you can download Ruby from http://rubyforge.org/frs/?group_id=167 for L ...

  9. Principles of good RESTful API Design 好的 RESTful API 设计

    UPDATE: This post has been expanded upon and converted into an eBook. Good API design is hard! An AP ...

  10. FreeMarker中文API手冊(完整)

    FreeMarker概述 FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写 FreeMarker被设计用来生成HTML Web页面,特别是基于MVC模式的应用 ...

随机推荐

  1. 代码页(CodePage)列表

    代码页编号    国家地区或语言37                 IBM037    IBM EBCDIC (US-Canada)    437                 IBM437    ...

  2. window.open()打开新窗口教程

    使用 window 对象的 open() 方法可以打开一个新窗口.用法如下: window.open (URL, name, features, replace) 参数列表如下: URL:可选字符串, ...

  3. 关于Mysql数据库建库字符集utf8mb4下,排序规则utf8mb4_bin和utf8mb4_general_ci选择造成的查询匹配大小写问题

    场景描述: 项目采用了分库模式进行不同业务的开发,在共有的功能模块进行设计的时候采用主从库,或者各分库之中存在同样的库表结构,在使用过程中做库表同步的时候一定要保证库表所在的数据库的字符集和编码格式是 ...

  4. vue中computed/method/watch的区别

    摘要:本文通过官方文档结合源码来分析computed/method/watch的区别. Tips:本文分析的源码版本是v2.6.11,文章中牵涉到vue响应式系统原理部分,如果不是很了解,建议先阅读上 ...

  5. jmeter进行分布式压测过程与 注意事项

    jmeter命令行运行但是是单节点下的, jmeter底层用java开发,耗内存.cpu,如果项目要求大并发去压测服务端的话,jmeter单节点难以完成大并发的请求,这时就需要对jmeter进行分布式 ...

  6. C#高级编程第11版 - 第七章 索引

    [1]7.1 相同类型的多个对象 1.假如你需要处理同一类型的多个对象,你可以使用集合或者数组. 2.如果你想使用不同类型的不同对象,你最好将它们组合成class.struct或者元组. [2]7.2 ...

  7. RabbitMq消费者在初始配置之后进行数据消费

    RabbitMq消费者在初始配置之后进行数据消费 问题背景 在写一个消费rabbitmq消息的程序是,发现了一个问题,消费者的业务逻辑里面依赖这一些配置信息,但是当项目启动时,如果队列里面有积压数据的 ...

  8. 【PC Basic】CPU、核、多线程的那些事儿

    一.CPU与核的概念 1.半导体中名词[Wafer][Chip][Die]中文名字和用途 Wafer--晶圆 wafer 即为图片所示的晶圆,由纯硅(Si)构成.一般分为6英寸.8英寸.12英寸规格不 ...

  9. 应答流式RPC 请求流式RPC 向流式RPC 流式RPC的三种具体形式

    https://mp.weixin.qq.com/s/pWwSfXl71GQZ3KPmAHE_dA 用Python进行gRPC接口测试(二) 大帆船 搜狗测试 2020-02-07   上期回顾:用P ...

  10. Linux 文件搜索神器 find 实战详解,建议收藏!

    大家好,我是肖邦,这是我的第 10 篇原创文章. 在 Linux 系统使用中,作为一个管理员,我希望能查找系统中所有的大小超过 200M 文件,查看近 7 天系统中哪些文件被修改过,找出所有子目录中的 ...