UVA 11883 Repairing a Road(最短路径+暴力枚举)
2013-08-23 22:45
423 查看
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.
View Code
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#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) { //t - v * pow(a, -t) 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; //cout<<fpai(mid, v[i], a[i])<<endl; 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] ; 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]] ); 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]] ); } 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()); } }
View Code
相关文章推荐
- UVa 10603 Fill [暴力枚举、路径搜索]
- nyoj 7 街区最短路径问题 暴力枚举
- HDU 3268/POJ 3835 Columbus’s bargain(最短路径+暴力枚举)(2009 Asia Ningbo Regional)
- uva 10167 Birthday Cake(暴力枚举)
- hdu1596 find the safest road(最短路径floyd)
- Island of Logic uva 暴力枚举
- POJ1964/UVALive 3029 -City Game -求最大子矩形-(暴力枚举/dp)
- UVa 12169 - Disgruntled Judge ( 暴力枚举 )
- C - How Many Maos Does the Guanxi Worth (dj+枚举每个人)最短路径
- UVA.12716 GCD XOR (暴力枚举 数论GCD)
- UVA585- Triangles(暴力枚举)
- POJ 1062 枚举单源最短路径(dijkstra算法)
- uva 1416 Warfare And Logistics (最短路径树)
- UVA - 10125 - Sumsets (暴力枚举 + STL)
- Hdu 1596 find the safest road 变相最短路径 Dijstra算法
- hdoj 1598 find the most comfortable road 【并查集】+【暴力枚举】
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- UVA 725 Divsion(暴力枚举)
- find the safest road - 最短路径问题(floyd算法)
- Dijkstra含权图最短路径;审判,不要错过枚举退款保证不会重复;国际象棋八皇后问题