湖南省第6届程序大赛 Repairing a Road
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.5891.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 ;
}
湖南省第6届程序大赛 Repairing a Road的更多相关文章
- 湖南省第6届程序大赛第6题 Biggest Number
Problem F Biggest Number You have a maze with obstacles and non-zero digits in it: You can start fro ...
- csu 1503: 点弧之间的距离-湖南省第十届大学生计算机程序设计大赛
这是--比量p并用交点连接中心不上弧.在于:它至p距离.是不是p与端点之间的最短距离 #include<iostream> #include<map> #include< ...
- 2016 "Bird Cup" ICPC7th@ahstu--“波导杯”安徽科技学院第七届程序设计大赛
"波导杯"安徽科技学院第七届程序设计大赛 Contest - 2016 "Bird Cup" ICPC7th@ahstu Start time: 2016-0 ...
- 2016 "Bird Cup" ICPC7th@ahstu--“波导杯”安徽科技学院第七届程序设计大赛
"波导杯"安徽科技学院第七届程序设计大赛 原文章网页 Contest - 2016 "Bird Cup" ICPC7th@ahstu Start time: ...
- 摹客 · Veer 第二届设计大赛邀你来战!
2018年12月,摹客设计大赛一年一度一归来. 继2017年摹客全国首届原型设计大赛成功举办后,本次大赛是摹客第二届设计大赛.大赛由摹客主办,Veer独家冠名赞助,iSlide和创客贴协办,国内多家知 ...
- 湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)
Biggest Number 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero di ...
- 湖南省第十三届大学生计算机程序设计竞赛 Football Training Camp 贪心
2007: Football Training Camp[原创-转载请说明] Submit Page Summary Time Limit: 1 Sec Memory Limit: 1 ...
- YTU 2547: Repairing a Road
2547: Repairing a Road 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: 2 题目描述 You live in a small town with R b ...
- 湖南省第六届大学生程序设计大赛原题 F Biggest Number (UVA1182)
Biggest Number http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30851#problem/F 解题思路:DFS(检索)+BF ...
随机推荐
- test20190925 老L
100+0+0=100.概率题套路见的太少了,做题策略不是最优的. 排列 给出 n 个二元组,第 i 个二元组为(ai,bi). 将 n 个二元组按照一定顺序排成一列,可以得到一个排列.显然,这样的排 ...
- Haskell语言学习笔记(94)Enum Bounded
Enum class Enum a where succ, pred :: a -> a toEnum :: Int -> a fromEnum :: a -> Int enumFr ...
- CSP-J2019游记&解题报告
考前一天晚上失眠.......(其实主要不是因为考试的原因) 很幸运,我们学校就是一个考点,本场作战,应该有一点加持吧. 上午在家复习,看到一篇关于PN532模拟小米手环加密卡的文章,于是,,,,,, ...
- LeetCode 919. Complete Binary Tree Inserter
原题链接在这里:https://leetcode.com/problems/complete-binary-tree-inserter/ 题目: A complete binary tree is a ...
- webpack的plugin原理
plugin是webpack生态的重要组成,它为用户提供了一种可以直接访问到webpack编译过程的方式.它可以访问到编译过程触发的所有关键事件. 1. 基本概念 1. 如何实现一个插件 1. plu ...
- 洛谷 P1250 种树 题解
差分约束系统,维护前缀和,根据式子d[ b ] < = d[ e + 1 ] - t,可以看出要连e和b - 1,但占用了超级源点0,所以要把区间向后移,这样就可以用超级源点0来保持图的连通性( ...
- 4、spark streaming+kafka
一.Receiver模式 1. receiver模式原理图 在SparkStreaming程序运行起来后,Executor中会有receiver tasks接收kafka推送过来的数据.数据会被持久化 ...
- 61、Spark Streaming:部署、升级和监控应用程序
一.部署应用程序 1.流程 1.有一个集群资源管理器,比如standalone模式下的Spark集群,Yarn模式下的Yarn集群等. 2.打包应用程序为一个jar包. 3.为executor配置充足 ...
- PKUWC2019 Round 2 没去祭
因为今年有两场 PKUWC,所以叫 PKUWC2019 Round 2. 因为一些沙雕原因,今年去不了了. Day 0 一如既往,没有集训就去上数学课,今天讲几何变换,一如既往的只会说"少女 ...
- CDH CM版本 6.0.1 升级到 CM 6.2.0 当前最新版本(CentOS 7.x)
CDH 的 6.0.1 是一个尴尬的版本,那时候 cloudera 还没有将 spark 更新到 2.4 还使用的是 spark 2.2版本. 但后来我们发现 2.3 | 2.4 更新了非常多的 fe ...