YTU 2547: Repairing a Road
2547: Repairing a Road
时间限制: 1 Sec 内存限制: 128 MB
提交: 3 解决: 2
题目描述
You live in a small town with R bidirectional roads connecting C crossings and you want to go from crossing 1 to crossing C as soon as possible. You can visit other crossings before arriving at crossing C, but it’s not mandatory.
You have exactly one chance to ask your friend to repair exactly one existing road, from the time you leave crossing 1. If he repairs the i-th road for t units of time, the crossing time
after that would be viai-t. It's not difficult to see that it takes vi units of time to cross that road if your friend doesn’t repair it.
You cannot start to cross the road when your friend is repairing it.
Input
There will be at most 25 test cases. Each test case begins with two integers C and R (2<=C<=100, 1<=R<=500). Each of the next R lines contains two integers xi, yi (1<=xi, yi<=C)
and two positive floating-point numbers vi and ai (1<=vi<=20,1<=ai<=5), indicating that there is a bidirectional road connecting crossing xi and yi, with parameters vi and ai (see above).
Each pair of crossings can be connected by at most one road. The input is terminated by a test case with C=R=0, you should not process it.
Output
For each test case, print the smallest time it takes to reach crossing C from crossing 1, rounded to 3 digits after decimal point. It’s always possible to reach crossing C from crossing 1.
输入
输出
样例输入
3 21 2 1.5 1.82 3 2.0 1.52 11 2 2.0 1.80 0
样例输出
2.5891.976
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
const int MAXN = 110;
const int MAXE = 1010;
const double EPS = 1e-6;
inline int sgn(double x)
{
if(fabs(x) < EPS) return 0;
return x > 0 ? 1 : -1;
}
double fpai(double t, double v, double a)
{
return 1 - log(a) * v * pow(a, - t);
}
inline void update_min(double &a, const double &b)
{
if(a > b) a = b;
}
double mat[MAXN][MAXN];
int x[MAXE], y[MAXE];
double v[MAXE], a[MAXE];
int n, m;
void floyd()
{
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j) update_min(mat[i][j], mat[i][k] + mat[k][j]);
}
double find_t(int i, int x, int y, double l, double r)
{
double L = l, R = r;
while(R - L > EPS)
{
double mid = (L + R) / 2;
if(fpai(mid, v[i], a[i]) > 0) R = mid;
else L = mid;
}
if(sgn(fpai(L, v[i], a[i])) != 0) return l;
return L;
}
double solve()
{
double t, ans = mat[1][n];
for(int i = 0; i < m; ++i)
{
t = find_t(i, x[i], y[i], mat[1][x[i]], ans);
update_min(ans, t + v[i] * pow(a[i], -t) + mat[y[i]][n]);
t = find_t(i, y[i], x[i], mat[1][y[i]], ans);
update_min(ans, t + v[i] * pow(a[i], -t) + mat[x[i]][n]);
}
return ans;
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == 0 && m == 0) break;
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j) mat[i][j] = 1e5;
mat[i][i] = 0;
}
for(int i = 0; i < m; ++i)
{
int aa, bb;
double cc;
scanf("%d%d%lf%lf", &aa, &bb, &cc, &a[i]);
x[i] = aa;
y[i] = bb;
v[i] = cc;
update_min(mat[aa][bb], cc);
update_min(mat[bb][aa], cc);
}
floyd();
printf("%.3f\n", solve());
}
}
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
const int MAXN = 110;
const int MAXE = 1010;
const double EPS = 1e-6;
inline int sgn(double x)
{
if(fabs(x) < EPS) return 0;
return x > 0 ? 1 : -1;
}
double fpai(double t, double v, double a)
{
return 1 - log(a) * v * pow(a, - t);
}
inline void update_min(double &a, const double &b)
{
if(a > b) a = b;
}
double mat[MAXN][MAXN];
int x[MAXE], y[MAXE];
double v[MAXE], a[MAXE];
int n, m;
void floyd()
{
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j) update_min(mat[i][j], mat[i][k] + mat[k][j]);
}
double find_t(int i, int x, int y, double l, double r)
{
double L = l, R = r;
while(R - L > EPS)
{
double mid = (L + R) / 2;
if(fpai(mid, v[i], a[i]) > 0) R = mid;
else L = mid;
}
if(sgn(fpai(L, v[i], a[i])) != 0) return l;
return L;
}
double solve()
{
double t, ans = mat[1][n];
for(int i = 0; i < m; ++i)
{
t = find_t(i, x[i], y[i], mat[1][x[i]], ans);
update_min(ans, t + v[i] * pow(a[i], -t) + mat[y[i]][n]);
t = find_t(i, y[i], x[i], mat[1][y[i]], ans);
update_min(ans, t + v[i] * pow(a[i], -t) + mat[x[i]][n]);
}
return ans;
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == 0 && m == 0) break;
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j) mat[i][j] = 1e5;
mat[i][i] = 0;
}
for(int i = 0; i < m; ++i)
{
int aa, bb;
double cc;
scanf("%d%d%lf%lf", &aa, &bb, &cc, &a[i]);
x[i] = aa;
y[i] = bb;
v[i] = cc;
update_min(mat[aa][bb], cc);
update_min(mat[bb][aa], cc);
}
floyd();
printf("%.3f\n", solve());
}
}
YTU 2547: Repairing a Road的更多相关文章
- 湖南省第6届程序大赛 Repairing a Road
Problem G Repairing a Road You live in a small town with R bidirectional roads connecting C crossing ...
- UVA 11883 Repairing a Road(最短路径+暴力枚举)
You live in a small town with R bidirectional roads connecting C crossings and you want to go from c ...
- 【CodeForces 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路
E. President and RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567 ...
- Codeforces Round #Pi (Div. 2) ABCDEF已更新
A. Lineland Mail time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛) H XOR
链接:https://www.nowcoder.com/acm/contest/116/H来源:牛客网 题目描述 Once there was a king called XOR, he had a ...
- 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)- XOR(二进制使用)
链接:https://www.nowcoder.com/acm/contest/116/H来源:牛客网 题目描述 Once there was a king called XOR, he had a ...
- Codeforces Educational Codeforces Round 15 D. Road to Post Office
D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...
- poj 3216 Repairing Company(最短路Floyd + 最小路径覆盖 + 构图)
http://poj.org/problem?id=3216 Repairing Company Time Limit: 1000MS Memory Limit: 131072K Total Su ...
随机推荐
- mysql jdbc驱动与java 版本对应关系
当使用某些密码套件时,Connector/J5.1需要JRE 1.8.x才能使用SSL/TLS连接到MySQL 5.6,5.7和8.0.
- 零基础入门Python数据分析,只需要看懂这一张图,附下载链接!
摘要 在做数据分析的过程中,经常会想数据分析到底是什么?为什么要做数据数据分析?数据分析到底该怎么做?等这些问题.对于这些问题,一开始也只是有个很笼统的认识. 最近这两天,读了一下早就被很多人推荐的& ...
- Uva 1103 古代象形符号(dfs求连通块, floodfill, 进制转换)
题意: 给定一个H行W列的字符矩阵(H<200, W < 50), 输入的是一个十六进制字符, 代表一行四个相邻的二进制, 1代表像素, 0代表没有像素. 然后要求判断输入的是以下哪些图形 ...
- bzoj1455左偏树裸题
#include <stdio.h> bool vi[1000010]; int n,de[1000010],ls[1000010],rs[1000010],va[1000010],fa[ ...
- ICPC模板排版工具
感谢参考:https://www.cnblogs.com/palayutm/p/6444833.html 额外安装texlive, ubuntu环境提供参考: 1.下载镜像包 https://mirr ...
- js用for...in 这种遍历的方式
var arr = new Array("first", "second", "third") for(var item in arr) { ...
- 动态规划法解最长公共子序列<算法分析>
一.实验内容及要求 1.要求按动态规划法原理求解问题: 2.要求在20以内整数随机产生两个序列数据: 3.要求显示随机产生的序列及最长公共子序列.二.实验步骤 1.随机产生数列: 2.输出随机序列: ...
- DD & E-app
DD & E-app 企业内部开发的E应用 前端 demo https://github.com/open-dingtalk docs https://open-doc.dingtalk.co ...
- hdu 2642二维树状数组 单点更新区间查询 模板题
二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...
- Spring Cloud(5):Hystrix的使用
熔断:类似生活中的保险丝,电流过大就会熔断 降级:类似生活中的旅行,行李箱只有那么大,所以要抛弃一些非必需的物品 熔断降级应用: 某宝双十一商品下单,用户量巨大,于是考虑抛弃相关商品推荐等模块,确保该 ...