ZOJ Problem Set - 1655

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


这道题目其实是最长路,一个重x的物品从i到j到k,所剩物品x(1-rate[i][j])(1-rate[j][k]).因此需要维护(1-rate)最大。
dijkstra可以算最长路,但图中不能有负权。此题极其特殊,因为它不是权之和,而是权之积,从题目中可以看到0<=1-rate<=1,因此一个数乘以一个0~1之间的数,越乘越小,同样可以证明dijkstra可以用来算这题的最长路。以上的证明方法,可以参考最短路的证明。学会一个算法,就应该触类旁通。这道题是很好的例子,从这题我知道了dijkstra可以算某些特殊条件的图的最长路。
注意:此题有平行边,并且不一定连通。

代码:
#include<cstdio>
#include<iostream>
#define Maxn 510
using namespace std;

double adj[Maxn][Maxn],dist[Maxn];
int x[Maxn],vis[Maxn];
const double inf=10000000;
void dijkstra(int u,int n){
    for(int i=1;i<=n;i++)
        dist[i]=adj[u][i],vis[i]=0;
    vis[u]=1;
    for(int i=1;i<n;i++){
        double maxx=-0.5;
        int v=u;
        for(int j=1;j<=n;j++)
            if(!vis[j]&&dist[j]>maxx)
                maxx=dist[j],v=j;
        vis[v]=1;
        for(int j=1;j<=n;j++)
            if(!vis[j]&&adj[v][j]!=-1&&dist[v]*adj[v][j]>dist[j])
                dist[j]=dist[v]*adj[v][j];
    }
    double ans=0;
    for(int i=1;i<n;i++)
        ans+=((dist[i]==-1)?0:dist[i])*x[i];
    printf("%.2f\n",ans);
}
int main()
{
    int n,m,a,b;
    double c;
    while(~scanf("%d%d",&n,&m)){
        for(int i=1;i<n;i++)
            scanf("%d",x+i);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                adj[i][j]=-1;
        while(m--){
            scanf("%d%d%lf",&a,&b,&c);
            adj[a][b]=adj[b][a]=max(adj[a][b],1-c);
        }
        dijkstra(n,n);
    }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值