zoj 1655 Transport Goods(最短路径)
2014-09-28 00:10
399 查看
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个城市给首都(第N个城市)支援物资,有M条路,走每条路要耗费一定百分比(相对于这条路的起点的物资)的物资。问给定N-1个城市将要提供的物资,和每条路的消耗百分比。求能送到首都的最多的物资数量
相当于求第n个城市到其余各个城市最“长”路径和。
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个城市给首都(第N个城市)支援物资,有M条路,走每条路要耗费一定百分比(相对于这条路的起点的物资)的物资。问给定N-1个城市将要提供的物资,和每条路的消耗百分比。求能送到首都的最多的物资数量
相当于求第n个城市到其余各个城市最“长”路径和。
#include <stdio.h> #include <stdlib.h> #include <algorithm> #include <queue> #include <string.h> #include <iostream> using namespace std; int n,m; double map[105][105]; int num[105]; void Dijkstra() { bool visit[105]; double lowwast[105]; memset(visit, false, sizeof(visit)); for (int i=0; i<n; i++) { lowwast[i]=map[n-1][i]; } visit[n-1]=true; for (int i=0; i<n-1; i++) { int k=-1; double max=-1; for (int j=0; j<n; j++) { if(!visit[j]&&max<lowwast[j]) { max=lowwast[j]; k=j; } } visit[k]=true; for (int j=0; j<n; j++) { if(!visit[j] && map[k][j]*max>lowwast[j]) { lowwast[j]=map[k][j]*max; } } } double sum=0; for (int i=0; i<n-1; i++) { sum+=num[i]*lowwast[i]; } printf("%0.2lf\n",sum); } int main() { while (cin>>n>>m) { for (int i=0; i<n-1; i++) { cin>>num[i]; } for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { map[i][j]=0; } map[i][i]=1; } for (int i=0; i<m; i++) { int a,b; double v; cin>>a>>b>>v; if(map[a-1][b-1]<1.0-v) { map[a-1][b-1]=map[b-1][a-1]=1.0-v; } } Dijkstra(); } return 0; }
相关文章推荐
- ZOJ-1082-Stockbroker Grapevine(最短路径)
- zoj 1655 Transport Goods(Dijkstra~~)
- ZOJ 3166题解 最短路径之Floyd算法
- ZOJ-1655 Transport Goods
- ZOJ 2864 多条最短路径
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
- zoj 2750 Idiomatic Phrases Game(最短路径)
- zoj 1891 - 传说中的简答题 - 最短路径 - dijkstra
- ZOJ 2864 多条最短路径
- ZOJ 1053 (Dijkstra 算法 最短路径 入门)
- ZOJ 1655 FZU 1125 Transport Goods
- 单源最短路径 ZOJ 1091 Knight Moves
- zoj 3088 spfa 最短&最长路径
- ZOJ 1655 Transport Goods 最短路变形
- POJ 2235 Frogger / UVA 534 Frogger /ZOJ 1942 Frogger(图论,最短路径)
- BellmanFord单源最短路径路径算法差分约束系统ZOJ–2770
- zoj 2760 How Many Shortest Path 【最短路 + 最大流】 【求边不重复最短路径条数】
- ZOJ1655 Transport Goods(Floyd)
- ZOJ-1655-Transport Goods
- POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom /ZOJ 1291 MPI Maelstrom (最短路径)