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 ...
随机推荐
- redis(以php代码为例)
备注:redis及phpredis扩展安装请查看:PHP典型功能与Laravel5框架开发学习笔记 redis具有原子性,所以在高并发情况下确保数据的一致性 一.连接 $redis = new Red ...
- linux命令 host-常用的分析域名查询工具
博主推荐:更多网络测试相关命令关注 网络测试 收藏linux命令大全 host命令是常用的分析域名查询工具,可以用来测试域名系统工作是否正常. 语法 host(选项)(参数) 选项 -a:显示详细的 ...
- Mybatis 处理日期格式自动转换
java.lang.String和java.util.Date之间自动转换 @DateTimeFormat(pattern="yyyy-MM-dd")//页面写入数据库时格式化 @ ...
- SPOJ GSS6 Can you answer these queries VI
Can you answer these queries VI Time Limit: 2000ms Memory Limit: 262144KB This problem will be judge ...
- 九度oj 题目1190:大整数排序
题目1190:大整数排序 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4142 解决:1867 题目描述: 对N个长度最长可达到1000的数进行排序. 输入: 输入第一行为一个整数N,( ...
- 编程数学-∑(求和符号)-Sigma
百度百科:∑ 在数学中,我们把它作为求和符号使用. 大写Σ用于数学上的总和符号,比如:∑Pi,其中i=1,2,...,T,即为求P1 + P2 + ... + PT的和.小写σ用于统计学上的标准差.西 ...
- NYOJ-769乘数密码,逆元解法;
乘数密码 时间限制:1000 ms | 内存限制:65535 KB 难度:1 -> Link <- 简单代替密码的第二种,比移位密码稍微复杂点,不过鉴于NYOJ,是完全可以 ...
- [SCOI2008]奖励关 - 状压动规 - 概率与期望
Description 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝 ...
- 【HDOJ3341】Lost's revenge(AC自动机,DP)
题意:给出一个n个模式串,一个目标串,问把目标串重新排位最多能产生多少个模式串,可以重叠且所有串只包含A C G T. n<=10,len[i]<=10 len(s)<=40 Cas ...
- POJ 2135_Farm Tour
题意: 从出发点到目的地往返一次,道路i连接着ai号和bi号,长度为ci.同一条路只能走一次,求最小路径长度. 分析: 如果没有往返,那么就是简单的最短路问题.如果规定严格从左到右,那么就是简单的双调 ...