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

————————————————————集训11.5的分割线————————————————————

前言:这道题WA到哭。。。。。。关键就是对于这是有向图还是无向图的判断。看到Roll神也WA哭过。

思路:为什么是无向图呢?一开始一直在想这个问题,后来我突然发现,"There are some roads between the cities and the goods must be transported along the roads.",两个城市之间有路,不就是可以通行吗。常识题?所以只要边权是比率,图就是无向的。但是,,,如果你把货物残存量当作边权,就有向了(基数不一样乘积不同),就做不出来。(虽然我想都没想就把比率当作边权了)

认识到这个之后,就容易了。cost是损失率,转换成剩余率。要使得最后的物质最多,就要求最长路。跑一遍Dijkstra。

P.S. 首先,数学老师没死早的话,比率应该是累乘的。这题最坑的不是无向,是重边。长点记性吧!

代码如下:

/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
const int N = 105;
double G[N][N], dis[N];
int goods[N], n, m;
bool vis[N];

void Dijkstra()
{
	memset(vis, 0, sizeof(vis));
	memset(dis, 0, sizeof(dis));
	dis[n] = 1;//首都0损失
	for(int i = 0; i < n-1; i++) {
		double maxi = 0;//最长路
		int cur;
		for(int k = 1; k <= n; k++) if(!vis[k]) {
			if(maxi < dis[k]) {
				maxi = dis[k];
				cur = k;
			}
		}
		vis[cur] = 1;
		for(int k = 1; k <= n; k++) if(!vis[k]) {
			if(dis[k] < dis[cur] * G[cur][k]) {
				dis[k] = dis[cur] * G[cur][k];//注意!乘法!
			}
		}
	}
}

int main()
{
#ifdef J_Sure
	freopen("222.in", "r", stdin);
//  freopen(".out", "w", stdout);
#endif
	while(~scanf("%d%d", &n, &m)) {
		for(int i = 1; i < n; i++) {
			scanf("%d", &goods[i]);
		}
		int u, v;
		double w;
		memset(G, 0, sizeof(G));
		for(int i = 0; i < m; i++) {
			scanf("%d%d%lf", &u, &v, &w);
			if(G[u][v] < 1 - w)//重边取最优,卧槽
				G[u][v] = G[v][u] = 1 - w;
		}
		Dijkstra();
		double ans = 0;
		for(int i = 1; i < n; i++) {
			ans += 1.0*goods[i] * dis[i];
		}
		printf("%.2f\n", ans);
	}
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值