码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 1655 Transport Goods (最短路变形)

时间:2014-08-21 21:13:34      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   strong   for   ar   

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


货物在每条路都有一定比率的损耗,求损耗率比较低的路径即可


#include"stdio.h"
#include"string.h"
#include"iostream"
#include"algorithm"
using namespace std;
#define N 105
const int inf=-1;
double g[N][N],dis[N];
int w[N],mark[N];
double max(double a,double b)
{
    return a>b?a:b;
}
void dijkstra(int n)
{
    int i,s=n;
    double min;
    for(i=1;i<=n;i++)
        dis[i]=g[s][i];
    memset(mark,0,sizeof(mark));
    mark[s]=1;
    while(1)
    {
        min=inf;
        for(i=1;i<=n;i++)
        {
            if(!mark[i]&&min<dis[i])
            {
                min=dis[i];
                s=i;
            }
        }
        if(min==inf)
            break;
        mark[s]=1;
        for(i=1;i<=n;i++)
        {
            if(!mark[i]&&dis[i]<dis[s]*g[s][i])
                dis[i]=dis[s]*g[s][i];
        }
    }
    double ans=0;
    for(i=1;i<=n;i++)
        ans+=w[i]*dis[i];
    printf("%.2f\n",ans);
}
int main()
{
    int i,u,v,n,m;
    double c;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        for(i=1;i<n;i++)
            scanf("%d",&w[i]);
        memset(g,0,sizeof(g));
        for(i=0;i<m;i++)
        {
            scanf("%d%d%lf",&u,&v,&c);
            g[u][v]=g[v][u]=max(g[u][v],1-c);
        }
        dijkstra(n);
    }
    return 0;
}





zoj 1655 Transport Goods (最短路变形),布布扣,bubuko.com

zoj 1655 Transport Goods (最短路变形)

标签:style   blog   color   os   io   strong   for   ar   

原文地址:http://blog.csdn.net/u011721440/article/details/38736201

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!