ACM第七次测验(图论)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17882 | Accepted: 6363 |
Description
设从下往上数第i(1 <= i <= M)层蛋糕是半径为Ri, 高度为Hi的圆柱。当i < M时,要求Ri > Ri+1且Hi > Hi+1。
由于要在蛋糕上抹奶油,为尽可能节约经费,我们希望蛋糕外表面(最下一层的下底面除外)的面积Q最小。
令Q = Sπ
请编程对给出的N和M,找出蛋糕的制作方案(适当的Ri和Hi的值),使S最小。
(除Q外,以上所有数据皆为正整数)
Input
Output
Sample Input
100 2
Sample Output
68
Hint
体积V = πR2H
侧面积A' = 2πRH
底面积A = πR2
#include <stdio.h>
#define INF 0xFFFFFF
#define POW(x) ( (x) * (x) )
//计算∑n^3 = n^2 * n^2 * (n+1)^2 * (n+1)^2 * 4
#define SUM3(x) ( ( POW( (x) ) * POW( (x) + 1 ) ) >> 2 )
int V, F;//输入的总体积和蛋糕的层数
int ans;//最终的最小面积,也是搜索过程中的当前最小面积,搜索中不断更新 int
min( int a, int b ) { return a < b ? a : b;
} void
dfs( int cur_f, int lft_v, int udr_s, int cur_r, int cur_h ) { //cur_f,当前所处的层,从下往上进行搜索 //lft_v,当前还剩下多少体积没拼完 //udr_s,under_s,当前层以下(就是之前)已经拼出的表面积 int r, h;//当前层试探的半径和高度 int cir_s;//当前层的试探圆面积(由r决定) if ( !cur_f ) {//已经到达顶层(顶层是1,超出顶层了) //如果过剩余体积全部拼完,并且udr_s更优就跟新ans if ( !lft_v && udr_s < ans ) ans = udr_s; return ; } if ( udr_s >= ans ) return ;//剪枝1,如果当前拼出的面积以及大于前几次的最优解则不必再玩儿了 //剪枝2 //如果当前剩余体积连剩下可能的最小体积都达不到也退出 //因为题目要求下层一定要比上层半径和高度都大,因此一个m层的蛋糕,其最小体积为1^3 + 2^3...+ m^3 if ( lft_v < SUM3(cur_f) ) return; //剪枝3,如果当前拼出的面积加上剩下可能拼出的最小面积比之前拼出的最小面积还要大也不必再玩儿了 //剩余可能拼出的最小面积( lft_v << 1 ) / cur_r其实就是剩余体积除以当前层的最大面积,cur_r就是当前层可能的最大半径 //其实就是将剩下的体积看成一个整个圆柱,这实际上不符合题意,但是这里使用的是不等式的放缩 if ( ( udr_s + ( lft_v << 1 ) / cur_r ) >= ans ) return ; for ( r = cur_r; r >= cur_f; r-- ) {//从当前给定的半径开始搜索,半径最小不能炒股cur_f了 cir_s = r * r; if ( cur_f == F ) udr_s = cir_s;//如果当前层是底层,需要算上俯瞰面积 //题中底面积不要求,但是要求俯瞰面积 //剪枝4 //可以通过( lft_v - SUM3( cur_f - 1 ) ) / cir_s得到一个该层高度的上界,避免多余的搜索 //因为上面的所有层最大可能体积就是SUM3( cur_f - 1 ),所以当前层可能的最大体积就是lft_v - SUM3( cur_f - 1 ) //除以一下cir_s就是最大可能的高度 h = min( cur_h, ( lft_v - SUM3( cur_f - 1 ) ) / cir_s ); for ( ; h >= cur_f; h-- )//高度最小不能超过cur_f dfs( cur_f - 1, lft_v - cir_s * h, udr_s + ( ( r * h ) << 1 ), r - 1, h - 1 ); }
} int main() { ans = INF; scanf("%d%d", &V, &F); //由于总体积最大为10000 //极端情况下就一层,如果高度为1,则半径达到最大100,如果半径为1则高度达到最大10000 dfs( F, V, 0, 100, 10000 ); if ( INF == ans ) puts("0");//无解 else printf("%d\n", ans); return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3015 | Accepted: 1625 |
Description
This record defines a permutation P as follows: P(1) = 4, P(2) = 1, P(3) = 5, etc.
What is the value of the expression P(P(1))? It’s clear, that P(P(1)) = P(4) = 2. And P(P(3)) = P(5) = 3. One can easily see that if P(n) is a permutation then P(P(n)) is a permutation as well. In our example (believe us)
It is natural to denote this permutation by P2(n) = P(P(n)). In a general form the defenition is as follows: P(n) = P1(n), Pk(n) = P(Pk-1(n)). Among the permutations there is a very important one — that moves nothing:
It is clear that for every k the following relation is satisfied: (EN)k = EN. The following less trivial statement is correct (we won't prove it here, you may prove it yourself incidentally): Let P(n) be some permutation of an N elements set. Then there exists a natural number k, that Pk = EN. The least natural k such that Pk = EN is called an order of the permutation P.
The problem that your program should solve is formulated now in a very simple manner: "Given a permutation find its order."
Input
Output
Sample Input
5 4 1 5 2 3
Sample Output
6
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1010],cnt[10010],n,ans=1,maxn;
bool p[1010]; int gcd(int x,int y)
{ if(!(x%y)) return y; return gcd(y,x%y);
} int main()
{ int i,j; scanf("%d",&n); for(i=1;i<=n;++i) { scanf("%d",&a[i]); cnt[a[i]]++; maxn=max(maxn,a[i]); } for(i=1;i<=maxn;++i) cnt[i]+=cnt[i-1]; for(i=1;i<=n;++i) if(!p[i]) { j=i; int t=0; while(!p[j]) { t++; p[j]=1; j=cnt[a[j]];} ans=ans*t/gcd(ans,t); } printf("%d\n",ans); return 0;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 25202 | Accepted: 9856 |
Description
Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan -- i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as
1/Σ(to,td)d(to,td)
where the sum goes over all pairs of types in the derivation plan such that to is the original type and td the type derived from it and d(to,td) is the distance of the types.
Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan.
Input
Output
Sample Input
4 aaaaaaa baaaaaa abaaaaa aabaaaa 0
Sample Output
The highest possible quality is 1/3.
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
const int MAXN = 2001;
const int INF = 0x3fffffff;
int n;
char s[MAXN][8];
int dis[MAXN][MAXN];
int key[MAXN];
bool check[MAXN];
int Prim()
{memset(check, false, sizeof(bool)*n);int sum = 0, min_key = INF, i, j;for(i = 1; i < n; i++)key[i] = INF;key[0] = 0;for(i = 0; i < n; i++) //n times,每次拿出一个点来贪心{int curp;min_key = INF;for(j = 0; j < n; j++){if(!check[j] && key[j] < min_key){curp = j;min_key = key[j];}}sum += min_key; //找出当前最小边加入生成树check[curp] = true;for(j = 1; j < n; j++){if(!check[j] && dis[j][curp] < key[j])key[j] = dis[j][curp];}}return sum;
}
int main()
{int i, j, k;while(cin>>n, n != 0){memset(dis, 0, sizeof(dis));for(i = 0; i < n; i++)cin>>s[i];for(i = 0; i < n-1; i++){for(j = i+1; j < n; j++){for(k = 0; k < 7; k++){if(s[i][k] != s[j][k])dis[i][j] = (++dis[j][i]);}}}cout <<"The highest possible quality is 1/"<<Prim()<<"."<<endl;}return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10784 | Accepted: 2678 |
Description
While this is somewhat pointless with only a few dominoes, some people went to the opposite extreme in the early Eighties. Using millions of dominoes of different colors and materials to fill whole halls with elaborate patterns of falling dominoes, they created (short-lived) pieces of art. In these constructions, usually not only one but several rows of dominoes were falling at the same time. As you can imagine, timing is an essential factor here.
It is now your task to write a program that, given such a system of rows formed by dominoes, computes when and where the last domino falls. The system consists of several ``key dominoes'' connected by rows of simple dominoes. When a key domino falls, all rows connected to the domino will also start falling (except for the ones that have already fallen). When the falling rows reach other key dominoes that have not fallen yet, these other key dominoes will fall as well and set off the rows connected to them. Domino rows may start collapsing at either end. It is even possible that a row is collapsing on both ends, in which case the last domino falling in that row is somewhere between its key dominoes. You can assume that rows fall at a uniform rate.
Input
The following m lines each contain three integers a, b, and l, stating that there is a row between key dominoes a and b that takes l seconds to fall down from end to end.
Each system is started by tipping over key domino number 1.
The file ends with an empty system (with n = m = 0), which should not be processed.
Output
Sample Input
2 1 1 2 27 3 3 1 2 5 1 3 5 2 3 5 0 0
Sample Output
System #1 The last domino falls after 27.0 seconds, at key domino 2.System #2 The last domino falls after 7.5 seconds, between key dominoes 2 and 3.
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
const int MAXN = 2001;
const int INF = 0x3fffffff;
int n;
char s[MAXN][8];
int dis[MAXN][MAXN];
int key[MAXN];
bool check[MAXN];
int Prim()
{memset(check, false, sizeof(bool)*n);int sum = 0, min_key = INF, i, j;for(i = 1; i < n; i++)key[i] = INF;key[0] = 0;for(i = 0; i < n; i++) //n times,每次拿出一个点来贪心{int curp;min_key = INF;for(j = 0; j < n; j++){if(!check[j] && key[j] < min_key){curp = j;min_key = key[j];}}sum += min_key; //找出当前最小边加入生成树check[curp] = true;for(j = 1; j < n; j++){if(!check[j] && dis[j][curp] < key[j])key[j] = dis[j][curp];}}return sum;
}
int main()
{int i, j, k;while(cin>>n, n != 0){memset(dis, 0, sizeof(dis));for(i = 0; i < n; i++)cin>>s[i];for(i = 0; i < n-1; i++){for(j = i+1; j < n; j++){for(k = 0; k < 7; k++){if(s[i][k] != s[j][k])dis[i][j] = (++dis[j][i]);}}}cout <<"The highest possible quality is 1/"<<Prim()<<"."<<endl;}return 0;
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 45663 | Accepted: 13543 |
Description
为了方便起见,我们把所有的物品从1开始进行编号,酋长的允诺也看作一个物品,并且编号总是1。每个物品都有对应的价格P,主人的地位等级L,以及一系列的替代品Ti和该替代品所对应的"优惠"Vi。如果两人地位等级差距超过了M,就不能"间接交易"。你必须根据这些数据来计算出探险家最少需要多少金币才能娶到酋长的女儿。
Input
Output
Sample Input
1 4 10000 3 2 2 8000 3 5000 1000 2 1 4 200 3000 2 1 4 200 50 2 0
Sample Output
5250
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;
struct node
{ int price,level;
}c[105];
struct node1
{ int num,discount;
};
vector<node1>edge[105];
int n,m,result;
bool vis[105];
int dfs(int x,int sum,int max_level,int min_level)
{ result=min(result,sum); max_level=max(max_level,c[x].level); min_level=min(min_level,c[x].level); for(int i=0;i<edge[x].size();i++) { node1 temp=edge[x][i]; if(fabs(min_level-c[temp.num].level)<=m&&fabs(max_level-c[temp.num].level)<=m&&!vis[temp.num]) { vis[temp.num]=true; dfs(temp.num,sum+temp.discount+c[temp.num].price-c[x].price, max(max_level,c[x].level),min(min_level,c[x].level)); vis[temp.num]=false; } }
}
int main()
{ while(~scanf("%d %d",&m,&n)) { memset(vis,false,sizeof(vis)); memset(edge,0,sizeof(edge)); memset(&c,0,sizeof(&c)); for(int i=1;i<=n;i++) { int x; scanf("%d %d %d",&c[i].price,&c[i].level,&x); for(int j=0;j<x;j++) { node1 temp; scanf("%d %d",&temp.num,&temp.discount); edge[i].push_back(temp); } } result=c[1].price; vis[1]=true; dfs(1,c[1].price,c[1].level,c[1].level); printf("%d\n",result); } return 0;
} </span>
一个人的旅行
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 32891 Accepted Submission(s): 11331
接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路)
接着的第T+1行有S个数,表示和草儿家相连的城市;
接着的第T+2行有D个数,表示草儿想去地方。
#include <iostream> #include <stdio.h> #include <memory.h> #include <queue> #include <algorithm> using namespace std; const int INF = 99999999; const int N = 1005; int map[N][N], dist[N], g[N]; bool visit[N]; int t, s, d; void init() //初始化函数 { int i, j; for(i = 0; i < N; i++) for(j = 0; j < N; j++) if(i == j) map[i][j] = 0; else map[i][j] = INF; } void input() //输入函数 { int i, k, ti, tj, cost; while(t--) { scanf("%d %d %d", &ti, &tj, &cost); //输入两点距离 if(cost < map[ti][tj]) map[ti][tj] = map[tj][ti] = cost; } for(i = 0; i < s; i++) //与家相邻的距离为0 { scanf("%d", &k); map[0][k] = map[k][0] = 0; } for(i = 0; i < d; i++) //输入想去的地方 scanf("%d", &g[i]); } void spfa() //spfa求起点到所有其它点的最短路 { int i, now; memset(visit, false, sizeof(visit)); for(i = 0; i < N; i++) dist[i] = INF; dist[0] = 0; queue<int> Q; Q.push(0); visit[0] = true; while(!Q.empty()) { now = Q.front(); Q.pop(); visit[now] = false; for(i = 0; i < N; i++) { if(dist[i] > dist[now] + map[now][i]) { dist[i] = dist[now] + map[now][i]; if(visit[i] == false) { Q.push(i); visit[i] = true; } } } } } int main() { int i, MIN; while(scanf("%d %d %d", &t, &s, &d) != EOF) { init(); input(); spfa(); MIN = INF; for(i = 0; i < d; i++) //找所有想去的点中最短的 { if(dist[g[i]] < MIN) MIN = dist[g[i]]; } printf("%d\n", MIN); } return 0; }
ACM第七次测验(图论)相关推荐
- 要关闭python解释器可使用函数或者快捷键_超星尔雅中华传统文化之戏曲瑰宝第七章节测验网课答案选修课慕课答案...
超星尔雅中华传统文化之戏曲瑰宝第七章节测验网课答案选修课慕课答案 更多相关问题 [多选题]2018年6月初,某企业无形资产账面价值为1 000万元,采用直线法摊销.6月份发生相关业务如下: (1)1日 ...
- “玲珑杯”ACM比赛 Round #18 C -- 图论你先敲完模板【Dp】
C -- 图论你先敲完模板 Time Limit:5s Memory Limit:256MByte Submissions:660Solved:160 DESCRIPTION 今天HHHH在操场上跑步 ...
- python123第七周测验编程题答案_Python第七周编程题
Python123第七周编程题 1.打印输出附件文件的平均列数,计算方法如下: ...
- 思科网络安全 第七章测验答案
在哪种类型的攻击中,网络犯罪分子试图阻止合法用户访问网络服务? 选择一项: DoS MITM 会话劫持 地址欺骗 反馈 Refer to curriculum topic: 7.2.1 在 DoS 或 ...
- 思科 CCNA2 第七章测验答案
1.哪种云计算服务最适合需要协作创建应用并通过网络交付该应用的企业? 选择一项: PaaS IaaS SaaS ITaaS 反馈 Refer to curriculum topic: 7.2.1 平台 ...
- 图论入门六:哥尼斯堡七桥问题
转载自https://blog.csdn.net/saltriver/article/details/54585595 哥尼斯堡七桥问题: 1736年,年仅29岁的数学家欧拉来到普鲁士的古城哥尼斯堡( ...
- 图论入门一:图的基本概念
前言: 图(graph)并不是指图形图像(image)或地图(map).通常来说,我们会把图视为一种由"顶点"组成的抽象网络,网络中的各顶点可以通过"边"实现彼 ...
- [渝粤教育] 厦门理工学院 机械设计 参考 资料
教育 -机械设计-章节资料考试资料-厦门理工学院[] 第三单元测验 1.[单选题]在常用的螺旋传动中,传动效率最高的螺纹是 A.三角形螺纹 B.梯形螺纹 C.锯齿形螺纹 D.矩形螺纹 参考资料[ ] ...
- [渝粤教育] 山东第一医科大学 健康教育与健康促进 参考 资料
教育 -健康教育与健康促进-章节资料考试资料-山东第一医科大学[] 第一讲测验 1.[单选题]健康教育学是一门以什么为对象的科学和艺术? A.健康相关行为 B.健康 C.疾病 D.环境 E.教育 参考 ...
最新文章
- 【转】一文掌握 Linux 性能分析之网络篇(续)
- 柱状折线图2-双柱状重合堆积折线-重写图例点击事件
- 【读书笔记】程序员的自我修养总结(七)
- Codeforces 229D
- Kotlin入门(29)任务Runnable
- 团体程序设计天梯赛-练习集-L1-036. A乘以B
- DIY一套10倍\20倍\30倍光学变焦高清航拍方案(变焦云台相机方案)
- 软件测试(三)——lab 1
- 一个ExtJs的最基本的mvc模式示例
- TypeError: type ‘types.GenericAlias‘ is not an acceptable base type
- VBA代码宝+代码助手
- Oracle中文转拼音函数
- rhel centos 源_Rhel centos 7的fips脚本
- ubuntu9.10 添加bones7456源
- ye lynn yama Loafer 已发送,请注意查收
- Word2010中怎样压缩图片使文件变小
- 【机器学习入门系列】第二章 探索性分析
- 机器学习算法的要点(附 Python 和 R 代码)
- QQ跳转浏览器html源码,手机QQ打开网址提示跳转浏览器 源代码分享
- 直播程序源码功能技术详解