zoukankan      html  css  js  c++  java
  • ZOJ 1655 FZU 1125 Transport Goods

    迪杰斯特拉最短路径。

    1.every city must wait till all the goods arrive, and then transport the arriving goods together with its own goods to the next city.  这个条件貌似可以忽略掉。

    2.One city can only transport the goods to one city.  这个条件貌似也可以忽略掉,是一定的。

    例如:A物品和B物品在不同的城市,他们都往X城市运输,然而X城市到首都的最少损失的路必定只有一条的,所以2条件可以忽略。至于1条件,等和不等好像也没什么区别...... = =!

    综上所述:就是每个物品只管自己运输,算最短路,每条路的权值就是比例,算出每个城市到首都损耗最少的路即可。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    const int maxn = 105;
    double weight[maxn], ratio[maxn][maxn], e[maxn];
    int s[maxn];
    int main()
    {
        int n, m, i, j, u, v, ii;
        double cc;
        while (~scanf("%d%d", &n, &m))
        {
            memset(s, 0, sizeof(s));
            memset(ratio, 0, sizeof(ratio));
            for (i = 1; i <= n - 1; i++) scanf("%lf", &weight[i]);
            for (i = 1; i <= m; i++)
            {
                scanf("%d%d%lf", &u, &v, &cc);
                cc = 1 - cc;
                if (cc > ratio[u][v])
                {
                    ratio[u][v] = cc;
                    ratio[v][u] = cc;
                }
            }
            for (i = 1; i <= n; i++) e[i] = ratio[n][i];
            e[n] = 1; s[n] = 1;
            for (ii = 1; ii < n; ii++)
            {
                double  maxn = -1;
                int flag = 0, x;
                for (i = 1; i <= n; i++)
                {
                    if (!s[i] && (maxn<0 || maxn<e[i]))
                    {
                        flag = 1;
                        maxn = e[i];
                        x = i;
                    }
                }
                if (!flag) break;
                s[x] = 1;
                for (i = 1; i <= n; i++)
                if (!s[i] && ratio[x][i] != 0 && e[x] * ratio[x][i] > e[i])
                    e[i] = e[x] * ratio[x][i];            
            }
            double anss = 0;
            for (i = 1; i < n; i++) anss = anss + e[i] * weight[i];    
            printf("%.2f
    ", anss);
        }
        return 0;
    }
  • 相关阅读:
    向量积&&凸包算法
    K短路模板POJ 2449 Remmarguts' Date
    [USACO]奶牛抗议(DP+树状数组+离散化)
    [Uva1642]魔法Gcd(数论)
    [NOIP2012]疫情控制(二分答案+倍增+贪心)
    关于欧几里德算法(gcd)的证明
    旅行(LCA)
    [NOIP2015]运输计划(树上差分+LCA+二分)
    [USACO]奶牛博览会(DP)
    24.基于groovy脚本进行partial update
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4498089.html
Copyright © 2011-2022 走看看