zoj 1655 最短路 Transport Goods

来源:互联网 发布:普通java项目编译运行 编辑:程序博客网 时间:2024/06/01 13:41
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


n-1个城市送货到capital ,capital级第n个城市,每个城市有货物质量w[ i ],每两个城市之间运货物有损耗 ,dis为最小的 损耗,求最短路。

注意,最后求的是货物到达n时的总质量。


#include<iostream>#include<cstdio>#include<cstring>using namespace std;int w[101],n,m;double cmp[101][101];int v[101];double dis[101]; void dijkstra(int x)     ///地杰斯塔啦dijkstra{    int i,j,k;    double min_;    memset(v,0,sizeof(v));    for(i=1;i<=n;i++)        dis[i]= (i!=x?cmp[i][x]:0);       ///dis[n]=0;起点为n    v[x]=1;                    ///   v[n]=1;标记走过    for(i=1;i<n;i++)    {        min_=0x3f3f3f3f;        for(j=1;j<=n;j++)        {            if(!v[j]&&dis[j]<min_)       ///找到最小的损耗            {                min_=dis[j];                k=j;            }        }        v[k]=1;  /// 标记        for(j=1;j<=n;j++)          if(!v[j]&&dis[j]>(1-dis[k])*cmp[k][j]+dis[k])                    dis[j]=dis[k]+(1-dis[k])*cmp[k][j];    }}int main(){    int i,j,a,b;    double c;    while(scanf("%d%d",&n,&m)!=EOF)    {        for(i=1;i<n;i++)            scanf("%d",&w[i]);          ///   每个点的质量        for(i=1;i<n;i++)            for(j=i+1;j<=n;j++)              cmp[i][j]=cmp[j][i]=1;    ///   损耗赋值为1        while(m--)        {            scanf("%d%d%lf",&a,&b,&c);            if(c<cmp[a][b])                                 cmp[a][b]=cmp[b][a]=c;        }         dijkstra(n);        double ans=0;        for(i=1;i<n;i++)           ans+=(1-dis[i])*w[i];          ///  (1-损耗)*质量=送达时的质量        printf("%.2lf\n",ans);    }    return 0;}



0 0
原创粉丝点击