hdu4418——Time travel

Time travel

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1739    Accepted Submission(s): 394

Problem Description

Agent K is one of the greatest agents in a secret organization called Men in Black. Once he needs to finish a mission by traveling through time with the Time machine. The Time machine can take agent K to some point (0 to n-1) on the timeline and when he gets
to the end of the time line he will come back (For example, there are 4 time points, agent K will go in this way 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, ...). But when agent K gets into the Time machine he finds it has broken, which make the Time machine can‘t
stop (Damn it!). Fortunately, the time machine may get recovery and stop for a few minutes when agent K arrives at a time point, if the time point he just arrive is his destination, he‘ll go and finish his mission, or the Time machine will break again. The
Time machine has probability Pk% to recover after passing k time points and k can be no more than M. We guarantee the sum of Pk is 100 (Sum(Pk) (1 <= k <= M)==100). Now we know agent K will appear at the point X(D is the direction of the Time machine: 0 represents
going from the start of the timeline to the end, on the contrary 1 represents going from the end. If x is the start or the end point of the time line D will be -1. Agent K want to know the expectation of the amount of the time point he need to pass before
he arrive at the point Y to finish his mission.

If finishing his mission is impossible output "Impossible !" (no quotes )instead.

Input

There is an integer T (T <= 20) indicating the cases you have to solve. The first line of each test case are five integers N, M, Y, X .D (0< N,M <= 100, 0 <=X ,Y < 100 ). The following M non-negative integers represent Pk in percentile.

Output

For each possible scenario, output a floating number with 2 digits after decimal point

If finishing his mission is impossible output one line "Impossible !"

(no quotes )instead.

Sample Input

2
4 2 0 1 0
50 50
4 1 0 2 1
100

Sample Output

8.14
2.00

Source

2012 ACM/ICPC Asia Regional Hangzhou Online

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  5153 5152 5151 5150 5149

Statistic | Submit | Discuss
| Note

很明显的概率dp,设dp[i]表示在点i的时候达到终点的期望步数

dp[i] = p1 * (dp[i + 1] + 1) + p2 * (dp[i + 2] + 2) + ...+ pm * (dp[i + m] + m);

当然 dp[e] = 0;

但是由于方向有2个,所以我们要把反着走的状态变成正着走,也就是把路对称一下, 012345 -》 0123456789

这样 比如 4 走到 3就变成了6走到7,方向就固定了,然后就可以建立线性方程组求解了,注意有些点到不了,这个得先用bfs处理一下,顺便给每一个可到的点编号,然后高斯消元就行了

/*************************************************************************
    > File Name: hdu4418.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2014年12月29日 星期一 17时01分42秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 210;
const double eps = 1e-12;

double sum_p;
double mat[N][N], x[N];
double p[N];
bool free_x[N];
int ord[N];
int n, m, s, e, cnt;

int Gauss()
{
	int col, k, equ, var, max_r;
	int free_index, free_num;
	equ = cnt;
	var = cnt;
	k = col = 0;
	for (int i = 0; i < var; ++i)
	{
		free_x[i] = true;
		x[i] = 0;
	}
	for (; k < equ && col < var; ++k, ++col)
	{
		max_r = k;
		for (int i = k + 1; i < equ; ++i)
		{
			if (fabs(mat[i][col]) - fabs(mat[max_r][col]) > eps)
			{
				max_r = i;
			}
		}
		if (max_r != k)
		{
			for (int j = k; j < var + 1; ++j)
			{
				swap(mat[k][j], mat[max_r][j]);
			}
		}
		if (fabs(mat[k][col]) <= eps)
		{
			--k;
			continue;
		}
		for (int i = k + 1; i < equ; ++i)
		{
			double tmp = mat[i][col] / mat[k][col];
			for (int j = col; j < var + 1; ++j)
			{
				mat[i][j] -= tmp * mat[k][j];
			}
		}
	}
	//判断解的情况
	for (int i = k; i < equ; ++i)
	{
		if (fabs(mat[i][var]) > eps)
		{
			return 0;
		}
	}
	if (k < var)
	{
		for (int i = k - 1; i >= 0; --i)
		{
			free_num = 0;
			for (int j = 0; j < var; ++j)
			{
				if (fabs(mat[i][j]) > eps && free_x[j])
				{
					free_num++;
					free_index = j;
				}
			}
			if (free_num >  1)
			{
				continue;
			}
			double tmp = mat[i][var];
			for (int j = 0; j < var; ++j)
			{
				if (fabs(mat[i][j]) > eps && j != free_index)
				{
					tmp -= mat[i][j] * x[j];
				}
			}
			free_x[free_index] = false;
			x[free_index] = tmp / mat[i][free_index];
		}
		return var - k;
	}
	for (int i = var - 1; i >= 0; --i)
	{
		double tmp = mat[i][var];
		for (int j = i + 1; j < var; ++j)
		{
			if (fabs(mat[i][j]) > eps)
			{
				tmp -= x[j] * mat[i][j];
			}
		}
		x[i] = tmp / mat[i][i];
	}
	return 1;
}

void bfs()
{
	cnt = 0;
	queue <int> qu;
	memset (ord, -1, sizeof(ord));
	ord[s] = cnt++;
	while (!qu.empty())
	{
		qu.pop();
	}
	qu.push(s);
	while (!qu.empty())
	{
		int u = qu.front();
		qu.pop();
		for (int i = 1; i <= m; ++i)
		{
			if (p[i] <= eps)
			{
				continue;
			}
			int v = (i + u) % n;
			if (ord[v] == -1)
			{
				ord[v] = cnt++;
				qu.push(v);
			}
		}
	}
}

void build()
{
	for (int i = 0; i < n; ++i)
	{
		if (ord[i] == -1)
		{
			continue;
		}
		if (i == e || i == (n - e) % n)
		{
			mat[ord[i]][ord[i]] = 1;
			mat[ord[i]][cnt] = 0;
			continue;
		}
		mat[ord[i]][ord[i]] = 1.0;
		for (int j = 1; j <= m; ++j)
		{
			if (ord[(i + j) % n] == -1)
			{
				continue;
			}
			mat[ord[i]][ord[(i + j) % n]] -= p[j];
		}
		mat[ord[i]][cnt] = sum_p;
	}
}

int main()
{
	int t, D;
	scanf("%d", &t);
	while (t--)
	{
		sum_p = 0;
		memset (mat, 0, sizeof(mat));
		scanf("%d%d%d%d%d", &n, &m, &e, &s, &D);
		n = 2 * n - 2;
		for (int i = 1; i <= m; ++i)
		{
			scanf("%lf", &p[i]);
			p[i] /= 100.0;
			sum_p += i * p[i];
		}
		if (s == e || n == 1)
		{
			printf("0.00\n");
			continue;
		}
		if (D > 0)
		{
			s = (n - s) % n;
		}
		bfs();
		if (ord[e] == -1 && ord[(n - e) % n] == -1)
		{
			printf("Impossible !\n");
			continue;
		}
		build();
		if (!Gauss())
		{
			printf("Impossible !\n");
			continue;
		}
		printf("%.2f\n", x[ord[s]]);
	}
	return 0;
}
时间: 2024-10-09 23:23:00

hdu4418——Time travel的相关文章

UVA 1048 - Low Cost Air Travel(最短路)

UVA 1048 - Low Cost Air Travel 题目链接 题意:给定一些联票,在给定一些行程,要求这些行程的最小代价 思路:最短路,一张联票对应几个城市就拆成多少条边,结点表示的是当前完成形成i,在城市j的状态,这样去进行最短路,注意这题有坑点,就是城市编号可能很大,所以进行各种hash 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #in

Travel(最短路)

Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n1,2,…,n. Among n(n−1)2n(n−1)2 pairs of towns, mm of them are connected by bidirectional highway, which needs aa minutes to travel. The other pairs are connected b

Travel(HDU 5441 2015长春区域赛 带权并查集)

Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2404    Accepted Submission(s): 842 Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is tr

ural 1286. Starship Travel

1286. Starship Travel Time limit: 1.0 secondMemory limit: 64 MB It is well known that a starship equipped with class B hyperengine is able to travel from any planet to any other planet. But your starship got severe damage in the last travel and now i

hdu 4571 Travel in time (Floyd+记忆化搜索)

Travel in time Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1853    Accepted Submission(s): 374 Problem Description Bob gets tired of playing games, leaves Alice, and travels to Changsha alo

travel hamburg

The area west of Hamburg's central railway station is mainly a shopping area with the streets Spitaler Straße and Mönckebergstraße, leading to Hamburg's town hall. The building behind the city hall is Hamburg's House of Commerce (Börse). Between the

poj 3230 Travel(dp)

Description One traveler travels among cities. He has to pay for this while he can get some incomes. Now there are n cities, and the traveler has m days for traveling. Everyday he may go to another city or stay there and pay some money. When he come

hdu5431 Travel

Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are n cities and m bidirectional roads connecting the cities. Jack hates waiting too long on the bus, but he

pat1030. Travel Plan (30)

1030. Travel Plan (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help