zoukankan      html  css  js  c++  java
  • POJ 3621 Sightseeing Cows

    Sightseeing Cows

    Time Limit: 1000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 3621
    64-bit integer IO format: %lld      Java class name: Main
     

    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.

     

    Input

    * 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: L1i , L2i , and Ti

     

    Output

    * 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.

     

    Sample Input

    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

    Sample Output

    6.00

    Source

     
    解题:二分+spfa判负圈+01分数规划
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 1010;
    18 struct arc{
    19     int to,w,next;
    20     arc(int x = 0,int y = 0,int z = -1){
    21         to = x;
    22         w = y;
    23         next = z;
    24     }
    25 };
    26 arc e[100000];
    27 int head[maxn],cnt[maxn],happy[maxn],tot,n,p;
    28 double d[maxn];
    29 bool in[maxn];
    30 void add(int u,int v,int w){
    31     e[tot] = arc(v,w,head[u]);
    32     head[u] = tot++;
    33 }
    34 bool spfa(double cur){
    35     queue<int>q;
    36     for(int i = 1; i <= n; i++){
    37         d[i] = 0;
    38         in[i] = true;
    39         cnt[i] = 0;
    40         q.push(i);
    41     }
    42     while(!q.empty()){
    43         int u = q.front();
    44         q.pop();
    45         in[u] = false;
    46         for(int i = head[u]; ~i; i = e[i].next){
    47             if(d[e[i].to] > d[u] - happy[e[i].to] + cur*e[i].w){
    48                 d[e[i].to] = d[u] - happy[e[i].to] + cur*e[i].w;
    49                 if(!in[e[i].to]){
    50                     q.push(e[i].to);
    51                     in[e[i].to] = true;
    52                     if(++cnt[e[i].to] > n) return false;
    53                 }
    54             }
    55         }
    56     }
    57     return true;
    58 }
    59 int main() {
    60     int u,v,w;
    61     double high,low,mid;
    62     while(~scanf("%d %d",&n,&p)){
    63         for(int i = 1; i <= n; i++)
    64             scanf("%d",happy+i);
    65         memset(head,-1,sizeof(head));
    66         for(int i = tot = 0; i < p; i++){
    67             scanf("%d %d %d",&u,&v,&w);
    68             add(u,v,w);
    69         }
    70         low = 0;
    71         high = 100;
    72         double ans = 0;
    73         while(fabs(high - low) > 1e-3){
    74             mid = (high+low)/2.0;
    75             if(spfa(mid)) high = mid;
    76             else low = mid;
    77         }
    78         printf("%.2f
    ",low);
    79     }
    80     return 0;
    81 }
    View Code
  • 相关阅读:
    发布一个用于WinCE的矢量图控件
    [非原创]树和图的遍历
    对ZOJ第1146题的解答:LCDisplay
    [c#]可在任意位置弹出的BalloonTip
    windows程序开发中c++和c#的对照
    关于编译时的warning treated as error
    flyweight模式和图元几何变换
    i++和++i作为参数时的编译器处理方式分析~
    用小数数组计算E值(对ZOJ第1113题的解答)
    显示SendMessage和PostMessage的区别
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3997249.html
Copyright © 2011-2022 走看看