湖南省第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 ... 
随机推荐
- 在linux中使用Sqlplus命令登录MySQL,查看表并设置行数和宽度,使其正常显示
			在linux中使用sqlplus命令进入MySQL,设置行数和行宽 1) 查看目前的pagesize,默认是14: 1. show pagesize; 2. set pa ... 
- get_template_part()调用自定义模板|wordpress函数
			我们在用wordpress开发主题的时候,可能需要调用一段固定的代码比如左侧菜单,这段代码会在主页.栏目页.文章页用到,如果直接写入每个页面模板是没有问题,但是如果要改左侧菜单一个链接那就要改三次,很 ... 
- Django API接口FK ManyTo Many  模板
			Url from django.contrib import admin from django.urls import path, re_path from django.urls import i ... 
- Discrete Cosine Transform
			离散余弦变换 由于实信号傅立叶变换的共轭对称性,导致DFT后在频域中有一半的数据冗余.离散余弦变换(DCT)在处理实信号时比离散傅立叶(DFT)变换更具优势.在处理声音信号这类实信号时,DFT得到的结 ... 
- BZOJ 3689: 异或之 可持久化trie+堆
			和超级钢琴几乎是同一道题吧... code: #include <bits/stdc++.h> #define N 200006 #define ll long long #define ... 
- avalon用background-image不起作用,怎么来选取前几个的图片进行渲染
			<span ms-css="{backgroundImage: 'url('+item.image + ')'}" ms-for="($index,item) in ... 
- pycharm plot独立窗口显示
			import matplotlib.pyplot as plt ... plt.show() 进行如下设置: File->Settings->Tools->Python Scient ... 
- Linux crontab命令:循环执行定时任务(详解版)
			前面学习了 at 命令,此命令在指定的时间仅能执行一次任务,但在实际工作中,系统的定时任务一般是需要重复执行的.而 at 命令显然无法满足需求,这是就需要使用 crontab 命令来执行循环定时任务. ... 
- MongoDB 数据库创建删除
			在MongoDB数据库里面是存在有数据库的概念,但是没有模式(所有的信息都是按照文档保存的),保存数据的结构就是JSON结构,只不过在进行一些数据处理的时候才会使用到MongoDB自己的一些操作符号 ... 
- windows 10下启用docker的k8s集群
			安装Docker Desktop后,由于国内无法下载到Kubernete的文件,在Docker Desktop设置里勾选启用Kubernete一直显示”Kubernete is starting”状态 ... 
