标签归档:floyd

hdu 5137 How Many Maos Does the Guanxi Worth(floyd)

Problem Description

“Guanxi” is a very important word in Chinese. It kind of means “relationship” or “contact”. Guanxi can be based on friendship, but also can be built on money. So Chinese often say “I don’t have one mao (0.1 RMB) guanxi with you.” or “The guanxi between them is naked money guanxi.” It is said that the Chinese society is a guanxi society, so you can see guanxi plays a very important role in many things.

Here is an example. In many cities in China, the government prohibit the middle school entrance examinations in order to relief studying burden of primary school students. Because there is no clear and strict standard of entrance, someone may make their children enter good middle schools through guanxis. Boss Liu wants to send his kid to a middle school by guanxi this year. So he find out his guanxi net. Boss Liu’s guanxi net consists of N people including Boss Liu and the schoolmaster. In this net, two persons who has a guanxi between them can help each other. Because Boss Liu is a big money(In Chinese English, A “big money” means one who has a lot of money) and has little friends, his guanxi net is a naked money guanxi net — it means that if there is a guanxi between A and B and A helps B, A must get paid. Through his guanxi net, Boss Liu may ask A to help him, then A may ask B for help, and then B may ask C for help …… If the request finally reaches the schoolmaster, Boss Liu’s kid will be accepted by the middle school. Of course, all helpers including the schoolmaster are paid by Boss Liu.

You hate Boss Liu and you want to undermine Boss Liu’s plan. All you can do is to persuade ONE person in Boss Liu’s guanxi net to reject any request. This person can be any one, but can’t be Boss Liu or the schoolmaster. If you can’t make Boss Liu fail, you want Boss Liu to spend as much money as possible. You should figure out that after you have done your best, how much at least must Boss Liu spend to get what he wants. Please note that if you do nothing, Boss Liu will definitely succeed.

 

Input

There are several test cases.

For each test case:

The first line contains two integers N and M. N means that there are N people in Boss Liu’s guanxi net. They are numbered from 1 to N. Boss Liu is No. 1 and the schoolmaster is No. N. M means that there are M guanxis in Boss Liu’s guanxi net. (3 <=N <= 30, 3 <= M <= 1000)

Then M lines follow. Each line contains three integers A, B and C, meaning that there is a guanxi between A and B, and if A asks B or B asks A for help, the helper will be paid C RMB by Boss Liu.

The input ends with N = 0 and M = 0.

It’s guaranteed that Boss Liu’s request can reach the schoolmaster if you do not try to undermine his plan.

 

Output
For each test case, output the minimum money Boss Liu has to spend after you have done your best. If Boss Liu will fail to send his kid to the middle school, print “Inf” instead.

 

Sample Input
4 5
1 2 3
1 3 7
1 4 50
2 3 4
3 4 2
3 2
1 2 30
2 3 10
0 0

 

Sample Output
50
Inf
题意:给出一个n个节点,m条边的无向图。你现在可以删去一个点以及他的所有边,让1到n的最短路最大。n<=30,m<=1000
当时看了一下n才30,这直接枚举删去的点然后n^3的floyd就可以了啊。然后就没什么了。。。
总时间复杂度O(n^4)。

 

BZOJ 1774: [Usaco2009 Dec]Toll 过路费(floyd+排序)

Description

跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道。为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费。 农场中由N(1 <= N <= 250)片草地(标号为1到N),并且有M(1 <= M <= 10000)条 双向道路连接草地A_j和B_j(1 <= A_j <= N; 1 <= B_j <= N)。奶牛们从任意一片草 地出发可以抵达任意一片的草地。FJ已经在连接A_j和B_j的双向道路上设置一个过路费L_j (1 <= L_j <= 100,000)。 可能有多条道路连接相同的两片草地,但是不存在一条道路连接一片草地和这片草地本身。最 值得庆幸的是,奶牛从任意一篇草地出发,经过一系列的路径,总是可以抵达其它的任意一片 草地。 除了贪得无厌,叫兽都不知道该说什么好。FJ竟然在每片草地上面也设置了一个过路费C_i (1 <= C_i <= 100000)。从一片草地到另外一片草地的费用,是经过的所有道路的过路 费之和,加上经过的所有的草地(包括起点和终点)的过路费的最大值。 任劳任怨的牛们希望去调查一下她们应该选择那一条路径。她们要你写一个程序,接受K(1 <= K <= 10,000)个问题并且输出每个询问对应的最小花费。第i个问题包含两个数字s_i 和t_i(1 <= s_i <= N; 1 <= t_i <= N; s_i != t_i),表示起点和终点的草地。 考虑下面这个包含5片草地的样例图像: 从草地1到草地3的道路的“边过路费”为3,草地2的“点过路费”为5。 要从草地1走到草地4,可以从草地1走到草地3再走到草地5最后抵达草地4。如果这么走的话, 需要的“边过路费”为2+1+1=4,需要的点过路费为4(草地5的点过路费最大),所以总的花 费为4+4=8。 而从草地2到草地3的最佳路径是从草地2出发,抵达草地5,最后到达草地3。这么走的话,边 过路费为3+1=4,点过路费为5,总花费为4+5=9。

Input

* 第1行: 三个空格隔开的整数: N, M和K * 第2到第N+1行: 第i+1行包含一个单独的整数: C_i * 第N+2到第N+M+1行: 第j+N+1行包含3个由空格隔开的整数: A_j, B_j和L_j * 第N+M+2倒第N+M+K+1行: 第i+N+M+1行表示第i个问题,包含两个由空格隔开的整数s_i 和t_i

Output

* 第1到第K行: 第i行包含一个单独的整数,表示从s_i到t_i的最小花费。

Sample Input

5 7 2
2
5
3
3
4
1 2 3
1 3 2
2 5 3
5 3 1
5 4 1
2 4 3
3 4 4
1 4
2 3
 

Sample Output

8
9
这道题目需要求任意点对间的最短路,而且n只有250,所以自然可以用floyd,但是发现他的最短路计算还要加上整条路上的最大的点权值。但是在floyd的过程中并不知道哪个是最大点权,不能加进去计算。所以本来想加一维表示最大点的位置,但是这样就n^4了不可以接受。
但是floyd的过程实际上相当于依次用每个点作为中转点k来进行计算i和j这两端点间的最短路:dp[i][j][k]表示从i到j的路径上除了两个端点i和j之外,只经过编号1到k的点时的最短路。那这样的话把点权按权值排序,依次计算最短路的时候,只需要比较一下两个端点和k的权值大小就可以了。

 

BZOJ上的USACO 第一弹

听说刷BZOJ上的USACO题目比较涨rank,于是我也开始了开心的划水生活~在这里贴一些题目的代码和思路,如果感觉比较好的题目用★标注出来,也会单独写写。

现在做了几题:

50/50

终于填完了,写题写的好慢啊QAQ。再弄个第二弹。

继续阅读