CSU 1806 Toll(数学+最短路)
2016-09-13 17:17
423 查看
思路:学习了一波辛普森公式求积分 点击打开链接,然后就是套最短路模板就可以了....
坑点:是有向图,看成了无向图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
;
}
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 n cities and m 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 u and v, there is at most
one road from u 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 thei-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 n 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
Sample Output
坑点:是有向图,看成了无向图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
;
}
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 n cities and m 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 u and v, there is at most
one road from u 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 thei-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 n 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
相关文章推荐
- 猜价格
- 用Html5制作 汤姆猫 小游戏
- 关于logstash-out-mongodb插件说明
- 天纵智能软件快速开发平台签到插件
- css文档
- Windows环境下Apache服务器的下载、安装及配置
- 演示HashSet的使用
- springMVC+maven+mybatis+Intellij IDEA环境搭建
- SQL基础
- HDU-1285【拓补排序】
- iOS开发- 点击通知栏回到顶部的动画效果
- js绑定事件
- 类的无参方法(课后5)
- router设置
- 十二章 课后5
- 十二章 课后作业3
- SQL Server健康检查的重要性
- Java 获取当前文件.class 的相对或绝对路径
- 【转】MySQL 添加密码、改密码、忘记密码
- haporxy 负载elasticsearch