【比赛练习ac题】poj2253+hdu2717+poj2387+poj1258【解题报告】
来源:互联网 发布:linux jvm内存设置 编辑:程序博客网 时间:2024/06/16 16:00
A - Catch That Cow
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
#include<stdio.h>#include<iostream>#include<queue>#include<string.h>using namespace std;#define N 300010int book[2*N];int start1,end,sum;struct node{ int x,step;};void BFS(){ node now,start,next1,next2,next3; queue<node>Q; start.step = 0; start.x = start1; Q.push(start); book[start1] =1; while(!Q.empty()) { now = Q.front() ; Q.pop(); if(now.x == end) {//printf("%d\n",now.x ); sum = now.step ; return; } next1.x = now.x + 1; if(next1.x < N&&!book[next1.x]) { next1.step = now.step + 1; book[next1.x] = 1; Q.push(next1); } next2.x = now.x -1; if(next2.x >= 0&&!book[next2.x]) { next2.step = now.step + 1; book[next2.x ] = 1; Q.push(next2); } next3.x = now.x *2; if(next3.x >= 0&&next3.x < N&&!book[next3.x]) { next3.step = now.step +1; book[next3.x] = 1; Q.push(next3); } } return;}int main(){ while(scanf("%d%d",&start1,&end)!=EOF) { memset(book,0,sizeof(book)); BFS(); printf("%d\n",sum); } return 0;}
B - Til the Cows Come Home
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
5 51 2 202 3 303 4 204 5 201 5 100Sample Output
90Hint
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
#include<stdio.h>#include<string.h>#define N 2300#define inf 99999999int e[N][N],book[N],dis[N];int main(){ int t,n,t1,t2,t3,min,i,j,u; while(scanf("%d%d",&t,&n)!=EOF) { for( i = 1; i <= n; i ++) for(int j = 1; j <= n; j ++) if(i ==j) e[i][j] = 0; else e[i][j] = inf; memset(book,0,sizeof(book)); for( i= 1; i <= t; i ++) { scanf("%d%d%d",&t1,&t2,&t3); if(t3 <e[t1][t2]) { e[t1][t2] = t3; e[t2][t1] = t3; } } for( i = 1; i <= n; i ++) dis[i] = e[1][i]; book[1] = 1; for( i = 1; i <= n-1; i ++) { min = inf; for( j = 1; j <= n; j ++) { if(!book[j]&&dis[j]<min) { u = j; min = dis[j]; } } book[u] = 1; for( j = 1; j <= n; j ++) { if(e[u][j]<inf) if(!book[j]&&dis[j] > dis[u]+e[u][j]) dis[j] = dis[u]+e[u][j]; } } printf("%d\n",dis[n]); } return 0;}
D - Agri-Net
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
Output
Sample Input
40 4 9 214 0 8 179 8 0 1621 17 16 0
Sample Output
28题意:输入矩阵,连通每个点的最小权值。思路:prime算法。
#include<stdio.h>#include<string.h>#define N 1010#define inf 99999999int dis[N], book[N], n, e[N][N];int main(){ int i, s, sum, t, x, y, z, Min, j; while(~scanf("%d", &n)){ memset(book, 0, sizeof(book)); for(i = 1; i <= n; i++){ for(j = 1; j <= n; j++){ scanf("%d", &e[i][j]); } } for(i = 1; i <= n; i++) dis[i] = e[1][i]; book[1] = s = 1; sum = 0; while(s < n){ Min = inf; for(i = 1; i <= n; i++) if(!book[i] && dis[i] < Min) Min = dis[i], j = i; s++; book[j] = 1; sum += dis[j]; for(i = 1; i <= n; i++){ if(!book[i] && dis[i]>e[j][i]) dis[i] = e[j][i]; } } printf("%d\n", sum); } return 0;}
I - Frogger
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.
Input
Output
Sample Input
20 03 4317 419 418 50
Sample Output
Scenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414题意:先输入起点和终点的坐标,再输入n-2个坐标,输出从1到n,最大边权的最小值(找到每条路径的最大值,再从这些路径中找到最小值)注意输出保留三位小数,每个样例后输出一个空行。思路:dijktra的变形题。每次比较出起点到当前路径的dis[u]值和顶点u到当前路径的值的e[u][j]的较大那个值,再判断当前路径的dis[j]是否大于较大值,若大于则更新。
#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#define inf 99999999#define N 250int book[N];int n,t,i,j,u;double min;double x,e[N][N],dis[N];struct node{ double x,y;};node w[N];int main(){ t = 0; while(scanf("%d",&n),n!=0) { t ++; scanf("%lf%lf",&w[1].x ,&w[1].y); scanf("%lf%lf",&w[n].x ,&w[n].y); for(i = 2; i <= n-1; i ++) scanf("%lf%lf",&w[i].x ,&w[i].y ); for(i = 1; i <= n; i ++) for(j = 1; j <= n; j ++) if(i == j) e[i][j] = 0; else e[i][j] = sqrt((w[i].x -w[j].x)*(w[i].x -w[j].x)+(w[i].y -w[j].y)*(w[i].y -w[j].y)); memset(book,0,sizeof(book)); for(i = 1; i <= n; i ++) { dis[i] = e[1][i]; } for(i = 1; i <= n-1; i ++) { min = inf; for(j = 1; j <= n; j ++) { if(!book[j]&&dis[j] < min) { min = dis[j]; u = j; } } book[u] = 1; for(j = 1; j <= n;j ++) { if(e[u][j] > dis[u]) x = e[u][j]; else x = dis[u]; if(!book[j]&&dis[j] > x) dis[j] =x; } } printf("Scenario #%d\n",t); printf("Frog Distance = %.3lf\n\n",dis[n]); } return 0;}
- 【比赛练习ac题】poj2253+hdu2717+poj2387+poj1258【解题报告】
- poj2253 解题报告
- poj1258解题报告
- POJ1258 解题报告
- poj2387解题报告(Dijkstra算法)
- POJ1258题解题报告
- 宁波比赛解题报告
- 比赛 解题报告
- 程设第二轮练习:垂直直方图 解题报告&AC代码
- 2011.11.2 poj1258 Agri-Net 解题报告
- AC Me解题报告
- AC自动机练习解题题集
- 拔河比赛[CODEVS1959]解题报告
- 2011.11.10 poj1062,poj2253,poj1125 单源最短路径 解题报告
- 某【并不能AC的】模拟题部分解题报告
- 2008湘潭大学程序设计比赛解题报告
- 深圳大学2012年程序设计比赛解题报告
- hdu 1285 确定比赛名次 解题报告
- SQL Server游标的使用
- linux查询删除文件清理空间.txt
- 【POJ】3009
- ListView自带单选和多选的特技Choice Mode
- 大数据Spark企业级实战版【学习笔记】----RDD:分布式函数式编程
- 【比赛练习ac题】poj2253+hdu2717+poj2387+poj1258【解题报告】
- Tensorflow 笔记
- 配置环境变量
- sql语句去除重复数据
- Red5流服务器搭建(实现在线直播,流媒体视频播放和在线视频会议)
- /dev/console,/dev/null,/dev/tty
- iSCSI remote block-level storage
- 探求ETL本质
- 各类存储器简介