ZOJ 1655 Transport Goods

The HERO country is attacked by other country. The intruder is attacking the capital so other cities must send supports to the capital. There are some roads between the cities and the goods must be transported along the roads.

According the length of a road and the weight of the goods, there will be some cost during transporting. The cost rate of each road is the ratio of the cost to the weight of the goods transporting on the road. It is guaranteed that the cost rate is less than
1.

On the other hand, every city must wait till all the goods arrive, and then transport the arriving goods together with its own goods to the next city. One city can only transport the goods to one city.

Your task is find the maximum weight of goods which can arrive at the capital.

Input

There are several cases.

For each case, there are two integers N (2 <= N <= 100) and M in the first line, where N is the number of cities including the capital (the capital is marked by N, and the other cities are marked from 1 to N-1), and M is the number of roads.

Then N-1 lines follow. The i-th (1 <= i <= N - 1) line contains a positive integer (<= 5000) which represents the weight of goods which the i-th city will transport to the capital.

The following M lines represent M roads. There are three numbers A, B, and C in each line which represent that there is a road between city A and city B, and the cost rate of this road is C.

Process to the end of the file.

Output

For each case, output in one line the maximum weight which can be transported to the capital, accurate up to 2 demical places.

Sample Input

5 6

10

10

10

10

1 3 0

1 4 0

2 3 0

2 4 0

3 5 0

4 5 0

Sample Output

40.00

额,最开始不怎么懂,想不好者类问题=。=

后来看了别人的,敲了个dijkstra,。。。

dis存的是最大的运输率,所以求最大的路径了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
using namespace std;
const int eps=1e-6;
const int maxn=110;
double dis[maxn];
double cost[maxn];
int visit[maxn];
double mp[maxn][maxn];
int n,m;
void dijkstra()
{
    int pos;
    memset(visit,0,sizeof(visit));
    for(int i=1;i<=n;i++)
        dis[i]=mp[n][i];
    dis[n]=1;
    visit[n]=1;//起点设为最大
    for(int i=1;i<=n;i++)
    {
        pos=-1;
        for(int j=1;j<=n;j++)
        {
            if(!visit[j]&&(pos==-1||dis[pos]<dis[j]-eps))//求最大的pos
                pos=j;
        }
        visit[pos]=1;
        for(int j=1;j<=n;j++)
        {
            if(!visit[j]&&dis[j]<dis[pos]*mp[pos][j]-eps)//更新最大路径
                dis[j]=dis[pos]*mp[pos][j];
        }
    }
}
int main()
{
    int u,v;
    double w;
    while(~scanf("%d%d",&n,&m))
    {
         for(int i=1;i<n;i++)
            scanf("%lf",&cost[i]);
         memset(mp,0,sizeof(mp));
         for(int i=1;i<=m;i++)
         {
             scanf("%d%d%lf",&u,&v,&w);
             if(mp[u][v]<1.0-w)
                mp[u][v]=mp[v][u]=1.0-w;//拿1.0减去它求最大路径
         }
         dijkstra();
         double sum=0.0;
         for(int i=1;i<n;i++)
             sum+=cost[i]*dis[i];
         printf("%.2f\n",sum);
    }
    return 0;
}

ZOJ 1655 Transport Goods

时间: 2024-10-24 23:06:21

ZOJ 1655 Transport Goods的相关文章

zoj 1655 Transport Goods (最短路变形)

Transport Goods Time Limit: 2 Seconds      Memory Limit: 65536 KB The HERO country is attacked by other country. The intruder is attacking the capital so other cities must send supports to the capital. There are some roads between the cities and the

Zoj 1655 Transport Goods 最短路的应用

这道题目真是充分显示了窝的智商低下,首先题目在有中文翻译的情况下看了半天没看懂,= = 然后已知这题的分类是最短路了还是不会做,= = 一开始想着在dij扩展的的时候就把最大运送值算好,不过后来发现正向运输和反向运输的值显然是不相等的o(╯□╰)o 后来直接把每个城市作为起点dij了,反正n就一百,n^3就n^3了,后来发现可以从多条路一起送到终点 无奈看了题解,发现原来dij算的应该是终点到各个点的最小费率(最大比率),这样不仅正向反向算结果是一样的,而且不会漏. 话说题目本来就是要把每个城市

zoj 1655 单源最短路 改为比例+最长路

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=655 没有理解清题意就硬套模板,所以WA了好几次, 解析看我的另一篇http://blog.csdn.net/u011026968/article/details/35579035 贴代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #de

[codeforces724E]Goods transportation

试题描述 There are n cities located along the one-way road. Cities are numbered from 1 to n in the direction of the road. The i-th city had produced pi units of goods. No more than si units of goods can be sold in the i-th city. For each pair of cities i

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛

Banana Bananas are the favoured food of monkeys. In the forest, there is a Banana Company that provides bananas from different places. The company has two lists. The first list records the types of bananas preferred by different monkeys, and the seco

POJ训练计划2516_Minimum Cost(网络流/费用流)

解题报告 题意: 有n个商店,m个提供商,k种商品</span> n*k的矩阵,表示每个商店需要每个商品的数目: m*k矩阵,表示每个提供商拥有每个商品的个数 然后对于每个物品k,都有n*m的矩阵 i行j列表示 从j提供商向i商店运送一个k商品的代价是多少 判断所有的仓库能否满足所有客户的需求,如果可以,求出最少的运输总费用 思路: 建图的题,不能直接把所有信息建成图,因为n和m跟k都有关系,如果那样子建图的话,就要把k种拆成m类,每个仓库连向该仓库的第k种,然后再和n连线,有费用, 不过这样

POJ 2516 Minimum Cost(最小费用最大流啊)

题目链接:http://poj.org/problem?id=2516 Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (mar

POJ 2516 Minimum Cost

Minimum Cost Time Limit: 4000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 251664-bit integer IO format: %lld      Java class name: Main Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In hi

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 F. Islands

On the mysterious continent of Tamriel, there is a great empire founded by human. To develope the trade, the East Empire Company is set up to transport goods from place to place. Recently, the company wants to start their business in Solstheim, which