NC25032. [USACO 2007 Dec G]Sightseeing Cows
描述
输入描述
* Line 1: Two space-separated integers: L and P
* Lines 2..L+1: Line i+1 contains a single one integer: Fi
* Lines L+2..L+P+1: Line L+i+1 describes cow path i with three space-separated integers: , , and Ti
输出描述
* Line 1: A single number given to two decimal places (do not perform explicit rounding), the maximum possible average fun per unit time, or 0 if the cows cannot plan any trip at all in accordance with the above rules.
示例1
输入:
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
说明:
The trip 1 -> 2 -> 3 -> 5 -> 1 has a total fun value of 60 and a length of 10 units for an average fun per unit time of 6. The trip 2 -> 3 -> 5 -> 2 only has an average fun per unit time of 30/6 = 5, and any trip involving landmark 4 has an average fun per unit time of less than 4.C++14(g++5.4) 解法, 执行用时: 44ms, 内存消耗: 560K, 提交时间: 2019-07-14 10:46:57
#include<bits/stdc++.h> using namespace std; typedef long long ll; struct edge{ int to,w,nex; }ed[5005]; int n,m,a[1005],u,v,w,head[1005],cnt,in[1005],mark[1005]; double dis[1005]; void add(int u,int v,int w){ ed[++cnt].to=v; ed[cnt].w=w; ed[cnt].nex=head[u]; head[u]=cnt; } queue<int>q; bool SPFA(double x){ while(!q.empty())q.pop(); for(int i=1;i<=n;i++){ mark[i]=in[i]=1,dis[i]=0; q.push(i); } while(!q.empty()){ int t=q.front(); q.pop(); mark[t]=0; for(int i=head[t];i;i=ed[i].nex){ int tp=ed[i].to; double w=a[t]-x*ed[i].w; if(dis[tp]<dis[t]+w){ dis[tp]=dis[t]+w; if(++in[tp]>n)return true; if(mark[tp])continue; q.push(tp); mark[tp]=1; } } } return false; } int main(){ while(~scanf("%d%d",&n,&m)){ memset(head,0,sizeof(head)); cnt=0; for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=m;i++){ scanf("%d%d%d",&u,&v,&w); add(u,v,w); } double l=0,r=1000,mid; while(r-l>1e-4){ mid=(l+r)/2; if(SPFA(mid))l=mid; else r=mid; } printf("%.2f\n",mid); } return 0; }
C++(clang++11) 解法, 执行用时: 124ms, 内存消耗: 504K, 提交时间: 2020-11-01 09:33:17
#include<cstdio> #include<algorithm> #include<queue> #define db double #define rep(i,x,y) for( int i=x;i<=y;i++) using namespace std; const int N=1005; struct node{ int y,z,next; }a[N*10]; int tot,head[N]; int n,m,b[N]; void add(int x,int y,int z){ a[++tot]=(node){y,z,head[x]}; head[x]=tot; } bool v[N]; db d[N]; int w[N]; bool check(db k){ queue<int>q; rep(i,1,n) q.push(i),d[i]=0,w[i]=1,v[i]=1; while (q.size()){ int x=q.front(); q.pop(); v[x]=0; for(int i=head[x];i;i=a[i].next){ int y=a[i].y; db dis=(db)a[i].z; if (d[y]>d[x]+k*dis-(db)b[x]){ d[y]=d[x]+k*dis-(db)b[x]; if (!v[y]){ q.push(y),v[y]=1; if (++w[y]>=n) return 1; } } } } return 0; } int main(){ scanf("%d%d",&n,&m); rep(i,1,n) scanf("%d",&b[i]); rep(i,1,m){ int x,y,z; scanf("%d%d%d",&x,&y,&z); add(x,y,z); } db l=0,r=1000010,mid; while (r-l>1e-4){ mid=(l+r)/2; if (check(mid)) l=mid; else r=mid; } printf("%.2lf",l); return 0; }