CSU 1806 Toll(数学+最短路)

本文介绍了一种结合辛普森法则与最短路径算法来解决随时间变化的路径费用问题的方法。具体而言,对于一个有向图,在特定的时间段内,每条边的费用会随着时间线性增长,目标是最小化从起点到终点的平均费用。通过使用Dijkstra算法找到不同时间点的最短路径,并利用辛普森法则进行数值积分,最终得到整个时间段内的平均最小费用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:学习了一波辛普森公式求积分 点击打开链接,然后就是套最短路模板就可以了....

坑点:是有向图,看成了无向图WA了好多次


#include<bits/stdc++.h>
using namespace std;
#define inf 1e9
#define eps 1e-8
int n,m,T;
struct Node
{
	int v,c,d;
	Node(){};
	Node(int vv,int cc,int dd):v(vv),c(cc),d(dd){};
};
vector<Node>e[15];
double dis[15];
struct node
{
	int x;
	double val;
	node(){};
	node(double a,int b):val(a),x(b){};
	bool operator <(const node&rhs)const
	{
		return val>rhs.val;
	}
};
double dijkstra(int s,double t)
{
	for(int i = 1;i<=n;i++)dis[i]=inf;
	dis[s]=0;
	priority_queue<node>q;
	q.push(node(0,1));
	while(!q.empty())
	{
		node a = q.top();q.pop();
		int u = a.x;
		for(int i = 0;i<e[u].size();i++)
		{
			int v = e[u][i].v;
			double tmp = 1.0*e[u][i].c*t+e[u][i].d;
			if(dis[v]>tmp+dis[u])
			{
				dis[v]=tmp+dis[u];
				q.push(node(dis[v],v));
			}
		}
	}
	return dis[n];
}
double gaogao(double a,double b)
{
	double fa = dijkstra(1,a);
	double fb = dijkstra(1,b);
	double fc = dijkstra(1,(a+b)/2);
	return (b-a)*(fa+fb+4*fc)/6;
}
double gao(double a,double b)
{
	double mid = (a+b)/2;
	double s0 = gaogao(a,b);
	double s1 = gaogao(a,mid);
	double s2 = gaogao(mid,b);
	if(fabs(s0-s1-s2)<eps)return s0;
	else
		return gao(a,mid)+gao(mid,b);
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&T)!=EOF)
	{
		for(int i = 0;i<=n;i++)
			e[i].clear();
        for(int i =1 ;i<=m;i++)
		{
			int u,v,cc,dd;
			scanf("%d%d%d%d",&u,&v,&cc,&dd);
			e[u].push_back(Node(v,cc,dd));
		}
		printf("%.8lf\n",gao(0,1.0*T)/T);
	}
}


Description

 In ICPCCamp, there are  cities and  unidirectional roads between cities. The  i-th road goes from the a  i-th city to the b  i-th city. For each pair of cities  and  v, there is at most one road from  to  v.
As traffic in ICPCCamp is becoming heavier, toll of the roads also varies. At time  t, one should pay (c  i⋅t+d  i) dollars to travel along the i-th road.
Bobo living in the 1-st city would like to go to the n-th city. He wants to know the average money he must spend at least if he starts from city 1 at  t∈[0,T]. Note that since Bobo's car is super-fast, traveling on the roads costs him  no time.
Formally, if  f(t) is the minimum money he should pay from city 1 to city  at time  t, Bobo would like to find

Input

The first line contains 3 integers n,m,T (2≤n≤10,1≤m≤n(n-1),1≤T≤10  4).
The i-th of the following m lines contains 4 integers a  i,b  i,c  i,d  i (1≤a  i,b  i≤n,a  i≠b  i,0≤c  i,d  i≤10  3).
It is guaranteed that Bobo is able to drive from city 1 to city n.

Output

 A floating number denotes the answer. It will be considered correct if its absolute or relative error does not exceed 10  -6.

Sample Input

3 3 2
1 2 1 0
2 3 1 0
1 3 1 1
3 3 2
1 2 1 0
2 3 1 0
1 3 0 5

Sample Output

1.75000000
2.00000000


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值