HDU1596 find the safest road

版权声明:转载请注明文章出处 https://blog.csdn.net/zhj_fly/article/details/73409820

find the safest road

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12829    Accepted Submission(s): 4566


Problem Description
XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条从u 到 v 的通道P 的安全度为Safe(P) = s(e1)*s(e2)…*s(ek) e1,e2,ek是P 上的边 ,现在8600 想出去旅游,面对这这么多的路,他想找一条最安全的路。但是8600 的数学不好,想请你帮忙 ^_^
 

Input
输入包括多个测试实例,每个实例包括:
第一行:n。n表示城市的个数n<=1000;
接着是一个n*n的矩阵表示两个城市之间的安全系数,(0可以理解为那两个城市之间没有直接的通道)
接着是Q个8600要旅游的路线,每行有两个数字,表示8600所在的城市和要去的城市
 

Output
如果86无法达到他的目的地,输出"What a pity!",
其他的输出这两个城市之间的最安全道路的安全系数,保留三位小数。
 

Sample Input
3 1 0.5 0.5 0.5 1 0.4 0.5 0.4 1 3 1 2 2 3 1 3
 

Sample Output
0.500 0.400 0.500

题意:中文题意不解释。

解析:典型最短路径问题,使用dijstra算法即可。此题权重是安全系数,且是相乘,要求安全系数越大越好。因为安全系数在[0, 1]之间,所以每乘一个系数,总体都会变小,与最短路径中每加一条边,总长度就会增加类似。具体见代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 1005
double mp[N][N];
double dis[N];
int vis[N];
int n;

void Dijstra(int start){
	for(int i = 1; i <= n; i++){
		dis[i] = mp[start][i];
		vis[i] = 0;
	}
	while(!vis[start]){
		vis[start] = 1;
		for(int i = 1; i <= n; i++){
			if(start == i || vis[i])continue;
			for(int j = 1; j <= n; j++){
				if(dis[j] < dis[i] * mp[i][j])
					dis[j] = dis[i] * mp[i][j];
			}
		}
		int Max = 0;
		for(int i = 1; i <= n; i++){
			if(!vis[i] && dis[i] > Max){
				start = i;
				Max = dis[i];
			}
		}
	}
}
int main(){
	while(~scanf("%d", &n)){
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= n; j++)
				scanf("%lf", &mp[i][j]);
		}
		int q, start, end;
		scanf("%d", &q);
		while(q--){
			scanf("%d%d", &start, &end);
			Dijstra(start);
			if(dis[end] == 0)
				puts("What a pity!");
			else
				printf("%.3lf\n", dis[end]);
		}
	}
	return 0;
}




阅读更多

Safest Points

04-16

Himekaidou Hatate (姫海棠はたて) is a crow tengu with the ability of spirit photography. The newspaper she writes, Kakashi Spirit News (「花果子念報」) is much less popular than Shameimaru Aya's Bunbunmaru News (「文々。新聞」). In order to beat Aya, Hatate decides to report Toramaru Shou, the disciple of Bishamonten (多闻天王). But it's not easy to take pictures of Shou and her bullet patterns while avoiding them as well.nnnAssum that both Hatate and Shou stay in a w*h grid region. Shou fire lasers out in straight lines, the i-th laser starts at (xi, yi) and moves in direction (dxi, dyi). The Manhattan distance of any two points (x1, y1) and (x2, y2) is |x1 - x2| + |y1 - y2|. Any point with a Manhattan distance to some laser that is less that 1 is called dangerous point. All other points are safe points. The points which have maximum Manhattan distance to dangerous points is called safest points. Hatate hopes that a new feature can be added to her camera - for each senario, finding out the safest points quickly. You're asked to develop this feature.nnInputnnThere are multiple cases. Each one describes a senario. The first line is "w h n", where 3 ≤ w, h ≤ 1000 and 1 ≤ n ≤ 1000 is the number of lasers. The i+1-th line is the i-th laser "xi yi dxi dyi", where 0 ≤ xi < w, 0 ≤ yi < h, dxi2 + dyi2 > 0. Process to the each of file.nnOutputnnFor each case, output all safest points seperated by a single blank in lexicographic order. If there are no safe points, output "MISS!" instead.nnSample Inputnn3 3 3n0 0 2 1n0 1 2 1n0 2 2 -2n4 4 2n0 0 3 3n3 0 -1 1n5 5 2n4 4 -1 -1n0 4 2 -2nSample OutputnnMISS!n(0, 1) (0, 2) (1, 0) (1, 3) (2, 0) (2, 3) (3, 1) (3, 2)n(0, 2) (2, 0) (2, 4) (4, 2)

Road

08-18

Problem DescriptionnIn the Green Heart of Holland the villages are small and the roads narrow. Some roads are only one car wide, so if two cars travelling in the opposite direction meet on such a road, they have a problem. In the Green Heart of Holland there usually is a canal at each side of the road, which makes it is impossible for a car to leave the road to let another car pass. To solve this problem,the roads here and there are made a bit wider, so one car can get aside, and one or more cars from the other side may pass. This works ne, as long as the trafic density is low. If, within a short period of time, a lot of cars from both sides try to pass the road, the tra c will get stuck. Given the number of cars entering at both sides (and the location of the passing places) there must be an optimal schedule, to have them all pass the road as soon as possible. Finding such a schedule may be quite hard, so we will not ask you to do so. We ask you to solve a more simple problem.nnGiven is a road (running East-West), with its passing points, a number of eastbound cars and a number of westbound cars. (An eastbound car enters the road at the West side). Furthermore a schedule is given, that is, for every pair (e,w), where e is an eastbound car and w is a westbound car, the place where they pass each other is given. If the cars enter the road with long delays, such a schedule may take an arbitrary long time. We assume, however, that all cars are ready to enter the road, and eager to leave the road as soon as possible. We measure the time between the moment the rst car enters the road, and the moment the last car leaves the road, and we want that interval to be as short as possible. All cars either stand still, or are driving with a constant speed of 45 km/h. Starting and stopping does not take any time. Two cars driving in the same direction always keep a distance of at least 25 meter. The distance between two different passing places is at least 30 meter. The length of the cars may be ignored.n nnInputnThe first line of input consists of the integer number n, the number of test cases;nThen, for each test case:nA line containing the length l (0 < l <=30000) of the road in meters, and the number p(0 < p) of passing places;nnA line containing p positive integers, the distance in meters from each passing place to the west end of the road. These numbers are sorted in increasing order; nnA line containing two positive integers e and w, (0 < e,w <= 1000) the number of eastbound and westbound cars respectively; nne lines, containing w numbers each, indicating the passing points of each pair of cars.The number z on position x (1 <=x<= w) of line y (1 <=y <= e) indicates that the eastbound car y passes the westbound car x at passing point z (0 <=z <= p + 1).Westbound car x enters the road before westbound car x + 1, and eastbound car y enters the road before eastbound car y + 1. The value z = 0 indicates crossing at the west end, so car y enters the road after x has left the road. The value z = p+1, in the same way, indicates crossing at the east side. nnAll numbers on a line are each separated by one or more spaces.n nnOutputnFor each test case, the output contains one line with one number: the time (in seconds, rounded to the nearest integer) it takes to have all cars pass the road.n nnSample Inputn2n150 1n50n1 1n1n100 1n30n3 2n2 2n1 2n0 2n nnSample Outputn16n48

没有更多推荐了,返回首页