Problem G

Repairing a Road

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.

Sample Input

Output for the Sample Input

3 2

1 2 1.5 1.8

2 3 2.0 1.5

2 1

1 2 2.0 1.8

0 0

2.589

1.976

题意:给C个顶点(从1开始编号), R条边, 的无向图. 其中每条边上有起点, 终点, 时间权值vi, 和另一参数ai。如果这道路总共修了t时间,那么这条路的花费就会变成 vi * ai ^ (-t)。我们要从1走到C,求最小花费(花费就是时间)。

思路: 先用floyd把每两点间的最短距离算出, 判断出这俩点是否相通,如果相通的话,暴力枚举每条可以用于节省时间的边。

 #include <iostream>
#include <cmath>
#include <cstdio>
#define LL long long
#include <cstring>
#define INF 100000000000
#include <queue>
#define eps (1e-6) using namespace std;
typedef struct edge
{
LL from,to;
double v,a;
}edge;
double cost[][];//floyd,cost 用于存储两点间的最短路
queue<edge> que;//用于暴力枚举每一条两点间的最短边
LL N,R;//N,R分别是顶点和边的数目
double sum;
void init()
{
while (!que.empty())
que.pop();
for (int i=;i<=N;i++)
{
for(int j=;j<=N;j++)
{
if (i==j)
cost[i][j]=;
else
cost[i][j]=INF;
}
}
}
void input()
{
edge e;
LL temp;
for (int i=;i<=R;i++)
{
scanf("%I64d %I64d %lf %lf",&e.from,&e.to,&e.v,&e.a);
if (e.v<cost[e.from][e.to])
cost[e.from][e.to]=e.v;//无向图,双向边
if (e.v<cost[e.to][e.from])
cost[e.to][e.from]=e.v;//不考虑修路效果情况下,最短路由e.v构成
que.push(e);
temp=e.from;
e.from=e.to;
e.to=temp;
que.push(e);
}
}
void floyd()
{
for (int k=;k<=N;k++)
{
for (int i=;i<=N;i++)
{
for (int j=;j<=N;j++)
{
cost[i][j]=min(cost[i][k]+cost[k][j],cost[i][j]);
}
}
}
}
void solve()
{
edge e;
double limit;//记录对于每条边递增函数的起点
while (!que.empty())
{
e=que.front();que.pop();
if (abs(e.a-)>eps)//防止出现除零错误
limit=log(e.v*log(e.a))/log(e.a);
else //数学计算:y = t + v*a^(-t); => y'=1+(-v*a^(-t)ln(a))=0; => a^t=v*ln(a); => t=log(a,v*ln(a))=log(e,v*ln(a))/log(e,a);
limit=;
if (cost[][e.from]<limit)
{ //sum = min( min(1->e.from) + f(e.from,e.to) + min(e.to->N) ) e:所有可能的边,考虑方向,所以e入队时做了处理
if (limit+e.v*pow(e.a,-limit)+cost[e.to][N] < sum)
sum=limit+e.v*pow(e.a,-limit)+cost[e.to][N];
}
else
{
limit=cost[][e.from];
if (limit+e.v*pow(e.a,-limit)+cost[e.to][N] < sum)
sum=limit+e.v*pow(e.a,-limit)+cost[e.to][N];
}
}
}
int main()
{
while (scanf("%I64d%I64d",&N,&R)==&&(R||N))
{
init();
input();
floyd();
sum=cost[][N];
solve();
printf("%.3lf\n",sum);
}
return ;
}

最新文章

  1. __getattr__与__getattribute__
  2. 不同版本strtotime(&quot;2016-09-04&quot;)输出不同问题
  3. Handler笔记
  4. PHP开发工具介绍之zendStudio
  5. 阻塞和非阻塞socket的区别
  6. NYOJ 914 Yougth的最大化
  7. OpenGL Development Cookbook chapter7部分翻译
  8. lua中的require机制
  9. [转载] Redis资料汇总专题
  10. Python基础学习参考(五):字符串和编码
  11. Node.js 部署免费/自动续订 HTTPS
  12. fullcalendar日历插件的使用并动态增删改查
  13. mysql case when * else end
  14. Java将Excel解析为数组集合
  15. Python+Selenium 利用ID,XPath,tag name,link text,partial link text,class name,css,name定位元素
  16. springboot(二十三)Springboot2.X响应式编程
  17. hihocoder 前两题思路
  18. python retrying retry
  19. centos7 python3.5中引入sqlite3
  20. itertools模块(收藏)

热门文章

  1. CentOS7.6使用Virt-manager创建虚拟机报错
  2. c++ main函数
  3. springboot的HTTPS配置
  4. js之大文件分段上传、断点续传
  5. dinoql 使用graphql 语法查询javascript objects
  6. REdis之maxmemory解读
  7. Hdu 3037 Saving Beans(Lucus定理+乘法逆元)
  8. 第03组 团队git现场编程实战
  9. 前端VScode推荐插件
  10. ICEM-圆角正方体