3月12日练习记录

最短路练习

Til the Cows Come Home

Title

Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

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.

Input

  • Line 1: Two integers: T and N

  • 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.

Output

  • Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.

Answer

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 1010;
const int inf = 0x3f3f3f3f;
int g[N][N],dist[N],vis[N];
int n,T;
void Dijk(){
	memset(dist,inf,sizeof dist);
	memset(vis,0,sizeof vis);
	dist[1] = 0;
	while(1){
		int t = -1,minn = inf;
		for(int j = 1;j <= n;j++){
			if(minn > dist[j] && !vis[j]){
				minn = dist[j];
				t = j;
			}
		}
		vis[t] = 1;
		if(t == -1) break;
		for(int j = 1;j <= n;j++){
			dist[j] = min(dist[j],dist[t]+g[t][j]);
		}
	}
}
int main()
{
	cin >> T >> n;
	memset(g,0x3f,sizeof g);
	while(T--){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		g[a][b] = g[b][a] = min(g[a][b],c);
	}
	Dijk();
	cout << dist[N] << endl;
	return 0;
}

Hint

最短路裸题,从n到1可以看成从1到n,由于数据量不大,可以用最简单版本的Dijkstra。

Frogger

Title

Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists’ sunscreen, he wants to avoid swimming and instead reach her by jumping.
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

The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy’s stone, stone #2 is Fiona’s stone, the other n-2 stones are unoccupied. There’s a blank line following each test case. Input is terminated by a value of zero (0) for n.

Output

For each test case, print a line saying “Scenario #x” and a line saying “Frog Distance = y” where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.

Answer

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 1010;
const int inf = 0x3f3f3f3f;
int x[N],y[N];
double g[N][N];
double dist[N];
bool st[N];
int n;
void Dijkstra(){
	memset(st,0,sizeof(st));
	for(int i = 1;i<=n;i++){
		 dist[i]=inf;
	}
	dist[1]=0;
	for(int i=1;i<=n-1;i++){
		int minn = inf;
        int u;
		for(int j=1;j<=n;j++){
            if(!st[j]&&minn>dist[j]){
                minn = dist[j];
                u = j;
            }		
        }	
		st[u]=1;
		for(int j = 1;j<=n;j++){
            dist[j]=min(dist[j],max(dist[u],g[u][j]));
        }
	}
}

int main(){
    int cas = 0;
	while(scanf("%d",&n)!=EOF){
		if(n==0) break;
		printf("Scenario #%d\n",++cas);
		for(int i = 1;i<=n;i++)
		    scanf("%d %d",&x[i],&y[i]);
		for(int i = 1;i<=n;i++)
		    for(int j=i+1;j<=n;j++)
		        g[i][j]=g[j][i]=sqrt(double(x[i]-x[j])*(x[i]-x[j])+double(y[i]-y[j])*(y[i]-y[j]));
		Dijkstra();
		printf("Frog Distance = %.3f\n\n",dist[2]);
	}
	return 0;
}

Hint

最短路问题变形,要求找到的是每条路径中的最大值,使得这个最大值最小化。

BFS练习

Dungeon Master

Title

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C.

Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!

Answer

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N = 35;
struct Node{
    int x,y,z;
};
bool vis[N][N][N];
char map[N][N][N];
int L,R,C;
struct Node begn;
struct Node ed;
struct Node cur;
struct Node nex;
int moveb[6][3] = {{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,1},{0,0,-1}};
int dist[N][N][N];
void BFS(){
    memset(dist,-1,sizeof dist);
    dist[begn.x][begn.y][begn.z] = 0;
    queue<Node> q;
    q.push(begn);
    while(!q.empty()){
        cur = q.front();
        q.pop();
        if(cur.x == ed.x && cur.y == ed.y && cur.z == ed.z){
            return ;
        }
        for(int i=0;i<6;i++){
            int x = cur.x + moveb[i][0];
            int y = cur.y + moveb[i][1];
            int z = cur.z + moveb[i][2];
            if(x>0 && x<=L && y>0 && y<=R && z>0 && z<=C && !vis[x][y][z] && map[x][y][z]!='#'){
                dist[x][y][z] = dist[cur.x][cur.y][cur.z] + 1;
                vis[x][y][z] = true;
                nex.x = x,nex.y = y,nex.z = z;
                q.push(nex);
            }
        }
        
    }
    
}
int main(){
    while(cin >> L >> R >> C){
        if(L==0 && R==0 && C==0) break;
        memset(vis,false,sizeof vis);
        for(int i=1;i<=L;i++){
            getchar();
            for(int j=1;j<=R;j++){
                for(int k=1;k<=C;k++){
                    cin >> map[i][j][k];
                    if(map[i][j][k] == 'S'){
                        begn.x = i;
                        begn.y = j;
                        begn.z = k;
                    }
                    else if(map[i][j][k] == 'E'){
                        ed.x = i;
                        ed.y = j;
                        ed.z = k;
                    }
                }
                getchar();
            }
        }
        BFS();
        if(dist[ed.x][ed.y][ed.z]==-1){
            cout << "Trapped!" << endl;
        }
        else{
            cout << "Escaped in " << dist[ed.x][ed.y][ed.z] << " minute(s)." << endl;
        }
    }
}

Hint

三维空间的BFS,可以用结构体。

Catch That Cow

Title

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

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Answer

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N = 100010;
bool vis[N];
int dist[N];
int be,ed;
void BFS(){
    dist[be] = 0;
    queue<int> q;
    q.push(be);
    while(!q.empty()){
        int t = q.front();
        if(t==ed) break;
        q.pop();
        if(t-1 >= 0 && !vis[t-1]){
            dist[t-1] = dist[t] + 1;
            q.push(t-1);
            vis[t-1] = true;
        }
        if(t+1 <= 100000 && !vis[t+1]){
            dist[t+1] = dist[t] + 1;
            q.push(t+1);
            vis[t+1] = true;
        }
        if(t*2 <= 100000 && !vis[t*2]){
            dist[t*2] = dist[t] + 1;
            q.push(t*2);
            vis[t*2] = true;
        }
    }
}
int main(){
    cin >> be >> ed;
    BFS();
    cout << dist[ed] << endl;
}

Hint

一维空间的BFS

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值