Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time.
Fortunately, they have a detailed city map showing the L (2 ≤ L ≤ 1000) major landmarks (conveniently numbered 1.. L) and the P (2 ≤ P ≤ 5000) unidirectional cow paths that join them. Farmer John will drive the cows to a starting landmark of their choice, from which they will walk along the cow paths to a series of other landmarks, ending back at their starting landmark where Farmer John will pick them up and take them back to the farm. Because space in the city is at a premium, the cow paths are very narrow and so travel along each cow path is only allowed in one fixed direction.
While the cows may spend as much time as they like in the city, they do tend to get bored easily. Visiting each new landmark is fun, but walking between them takes time. The cows know the exact fun values Fi (1 ≤ Fi ≤ 1000) for each landmark i.
The cows also know about the cowpaths. Cowpath i connects landmark L1i to L2i (in the direction L1i -> L2i ) and requires time Ti (1 ≤ Ti ≤ 1000) to traverse.
In order to have the best possible day off, the cows want to maximize the average fun value per unit time of their trip. Of course, the landmarks are only fun the first time they are visited; the cows may pass through the landmark more than once, but they do not perceive its fun value again. Furthermore, Farmer John is making the cows visit at least two landmarks, so that they get some exercise during their day off.
Help the cows find the maximum fun value per unit time that they can achieve.
Fortunately, they have a detailed city map showing the L (2 ≤ L ≤ 1000) major landmarks (conveniently numbered 1.. L) and the P (2 ≤ P ≤ 5000) unidirectional cow paths that join them. Farmer John will drive the cows to a starting landmark of their choice, from which they will walk along the cow paths to a series of other landmarks, ending back at their starting landmark where Farmer John will pick them up and take them back to the farm. Because space in the city is at a premium, the cow paths are very narrow and so travel along each cow path is only allowed in one fixed direction.
While the cows may spend as much time as they like in the city, they do tend to get bored easily. Visiting each new landmark is fun, but walking between them takes time. The cows know the exact fun values Fi (1 ≤ Fi ≤ 1000) for each landmark i.
The cows also know about the cowpaths. Cowpath i connects landmark L1i to L2i (in the direction L1i -> L2i ) and requires time Ti (1 ≤ Ti ≤ 1000) to traverse.
In order to have the best possible day off, the cows want to maximize the average fun value per unit time of their trip. Of course, the landmarks are only fun the first time they are visited; the cows may pass through the landmark more than once, but they do not perceive its fun value again. Furthermore, Farmer John is making the cows visit at least two landmarks, so that they get some exercise during their day off.
Help the cows find the maximum fun value per unit time that they can achieve.
样例输入
5 7
30
10
10
5
10
1 2 3
2 3 2
3 4 5
3 5 2
4 5 5
5 1 3
5 2 2
样例输出
6.00
二分答案,设答案为mid
则问题转化为存在环s,使得mid<点权和/边权和
则本题所求最大值大于Mid
式子变形为mid*边权和-点权和<0
每轮二分,将原边权设为mid*边权-点权,无点权。而
mid*边权和-点权和<0的含义就是存在负环。Spfa跑即可
#include <cstring> #include <cstdio> #include <queue> #include <map> using namespace std; typedef long long ll; const int maxn = 10055; struct Edge { int u,v,next; double w; }; struct Map { Edge edge[maxn]; int head[maxn]; int cnt; void init() { memset(head,-1, sizeof(head)); memset(edge,0, sizeof(edge)); cnt=0; } void addedge(int u,int v,double w) { edge[cnt].u=u; edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; } }Mp; int s; double dist[maxn]; int cnt[maxn]; int n; bool Spfa() { queue<int> q; bool vis[maxn]={0}; for(int i=1;i<=n;i++) { dist[i]=1e10; } q.push(s); vis[s]=true; dist[s]=0; while(!q.empty()) { int u=q.front(); vis[u]=false; q.pop(); for(int i=Mp.head[u];i!=-1;i=Mp.edge[i].next) { int v=Mp.edge[i].v; if(dist[v]>dist[u]+Mp.edge[i].w) { dist[v]=dist[u]+Mp.edge[i].w; cnt[v]=cnt[u]+1; if(cnt[v]>=n) return false; if(!vis[v]) { vis[v]=true; q.push(v); } } } } return true; } int a[maxn]; struct node { int u,v; double w; }nodes[maxn]; int main() { int m; //freopen("in.txt","r",stdin); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } for(int i=1;i<=m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); nodes[i].u=u; nodes[i].v=v; nodes[i].w=w; } double l=0,r=1e9,ans=0; while(r-l>1e-5) { double mid=(l+r)/2; memset(cnt,0, sizeof(cnt)); Mp.init(); for(int i=1;i<=m;i++) { double w=1.0*mid*nodes[i].w-1.0*a[nodes[i].u]; Mp.addedge(nodes[i].u,nodes[i].v,w); } s=1; if(Spfa()) { r=mid; ans=mid; } else l=mid; } printf("%.2lf ",ans); return 0; }