ZOJ-1655 Transport Goods
2014-08-11 22:45
363 查看
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. 首先,数学老师没死早的话,比率应该是累乘的。这题最坑的不是无向,是重边。长点记性吧!
代码如下:
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 , dis ; int goods , n, m; bool vis ; void Dijkstra() { memset(vis, 0, sizeof(vis)); memset(dis, 0, sizeof(dis)); dis = 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; }
相关文章推荐
- ZOJ-1655 Transport Goods 最短路
- ZOJ 1655 Transport Goods
- ZOJ1655 Transport Goods(Floyd)
- ZOJ1655 Transport Goods,Dijkstra算法
- ZOJ 1655 Transport Goods (杀入ranklist1000,撒花)
- ZOJ 1655 Transport Goods
- ZOJ 1655 Transport Goods 最短路变形
- zoj 1655 Transport Goods(最短路径)
- zoj 1655 Transport Goods
- zoj 1655 最短路 Transport Goods
- ZOJ 1655 Transport Goods(dijkstra最短路)
- ZOJ 1655 Transport Goods
- zoj 1655 Transport Goods (最短路变形)
- zoj 1655 Transport Goods(Dijkstra~~)
- ZOJ 1655 FZU 1125 Transport Goods
- zoj 1655 Transport Goods
- Zoj 1655 Transport Goods 最短路的应用
- ZOJ 1655 Transport Goods
- zoj 1655 Transport Goods(dijkstra)
- ZOJ-1655-Transport Goods