Three school joint training of Australian small gourd (calabash) solution to a problem

Questions surface:
small gourd Australia
[title] description
small Australian favorite song is "gourd doll."
Day show singing his diligence prehistoric, hearts sing hymns.
Following this, a small gourd Australia entered the world.
Calabash gourd world there are n, numbered 1 ~ n. n connected by the m gourd vines, each connected to the vine
two gourd, vines which constitute a directed acyclic graph. Each vine will climb a small Australian takes some energy.
Australia small stand (you can think of very large gourd, can withstand the weight of a small Australia), he was the No. 1 hoist
think along the vines to climb on n number hoists, each hoist through only once.
O find a small path, such that the ratio of the number of minimum energy consumption through the hoist.
[Input format
into a file called calabash.in.
The first line input two positive integers n, m, respectively represent the number hoist and the number of vines.
Next m lines of three positive integers u, v, w, a vine described, this represents a vine connected to v, u by a
small climb this vine O w consumes energy.
[] Output format
output file name calabash.out.
Line a real number, the answer (error less than 10 ^ -3).
[O] Sample
calabash.in calabash.out
. 4. 6
. 1 2. 1
2. 4. 6
. 1. 3 2
. 3. 4. 4
2. 3. 3
. 1. 4. 8
Example Description [2.000] O
has four climbing method:
1-> 4, 8 energy consumption, after two hoist, a ratio of 8/2 = 4.
1-> 2-> 4, the energy consumption of 1 + 6 = 7, after the hoist 3, the ratio of 7 / 3≈2.33.
1-> 3-> 4, the energy consumption of 2 + 4 = 6, after the hoist 3, the ratio is 6/3 = 2.
1-> 2-> 3-> 4, the energy consumption of 1 + 3 + 4 = 8, after four hoist, a ratio of 8/4 = 2.
So choose the third or fourth program, the answer is two.
[Agreed with the data size]
Test point number otherwise stated nm
1 2 1
2 1 100 99 In addition, the penetration of the hoist are all 1
. 3 100 105 all numbers from 1 to n hoist through equal path
. 4 1000 100
. 5 100 1000
. 6 199 198 other than 1, all of the hoist are 1
. 7 231 200 is the number of all the hoist from 1 to n via the path is equal
. 8 200 is 2000
. 9 2000 200 is
10 200 is 2000
for all data, each climb over small O vine energy consumption does not exceed 10 ^ 3, and there must be one from a
path to n.

algorithm1

         A first test point only one side, the output w / 2 can be friends.

       The first one can test point.

algorithm2

       注意到“除1外,所有葫芦的入度均为1”,也就是说,从1到n的路径只有一条,输出这一条路径的长度与这条路径上的点数的比值就可以了。

       可以通过第1、2、6个测试点。

algorithm3

       对于这样一类特殊数据,“所有从1到n的路径经过的葫芦数相等”,也就是说1~n的最短路就是最优路径,最短路的长度与路径上的点数的比值就是答案。

       可以通过第1、2、3、6、7个测试点。

algorithm4

       另建一个起点0,连接一条0到1长度为0的边,就此将问题转化为长度和边数最小比值。这个问题的求解需要分数规划。

       假设答案为ans,对于任意一条由k条边组成的路径,有:

       (w1+w2+w3+…+wk)/k>=ans;

       转化一下:

       (w1+w2+w3+…+wk) >=ans*k;

       即(w1-ans)+(w2-ans)+(w3-ans)+…+(wk-ans)>=0。

       于是就得到了这样一个算法:

       二分答案x,每次将每一条边的权值减去x求最短路,判断1~n的最短路是否大于0:若大于0,则说明答案ans>x;否则说明ans<x。

       这样可以通过所有测试点。

 

Guess you like

Origin www.cnblogs.com/kamimxr/p/11481329.html