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 300010 int 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 5 1 2 20 2 3 30 3 4 20 4 5 20 1 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 99999999 int 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
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28 题意:输入矩阵,连通每个点的最小权值。 思路:prime算法。
#include<stdio.h> #include<string.h> #define N 1010 #define inf 99999999 int 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
2 0 0 3 4 3 17 4 19 4 18 5 0
Sample Output
Scenario #1 Frog Distance = 5.000 Scenario #2 Frog 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 250 int 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; }