https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5091

题目大意:n个人编号从1到n,m条关系,a找b帮忙或b找a帮忙需要花费c元,当然a可以通过d找b帮忙(即a找d帮忙,d再找b帮忙)

现在1号要找n号帮忙,你需要去找1号和n号之外的人,让他不帮忙,即破坏这条关系链,如果1号最终能找n号b帮忙则输出过程中所

用的最大花费,否则输出Inf

分析:

转化为最短路问题:a到b的花费当做a点到b点的距离

破坏关系链只需在2—n-1号这n-3点中依次找每一个点,将该点到其他点和其他点到该点的距离令为INF,然后找一个1到n的最小距离,

得到n-3个距离,输出这n-3个距离中最大的距离

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std; const int N = ;
const int INF = 0x3f3f3f3f; int G[N][N], maps[N][N];
int n; void Init()
{
int i, j;
for(i = ; i <= n ; i++)
{
for(j = ; j <= n ; j++)
{
if(i == j)
G[i][j] = ;
else
G[i][j] = G[j][i] = INF;
}
}
} int Floyd(int m)
{
int i, j, k;
for(i = ; i <= n ; i++)
{
for(j = ; j <= n; j++)
{
if(i == m || j == m)
maps[i][j] = maps[j][i] = INF;//让编号为m的人不帮,来破坏该条关系链
else
maps[i][j] = maps[j][i] = G[i][j];
}
}
for(k = ; k <= n ; k++)
{
for(i = ; i <= n ; i++)
{
for(j = ; j <= n ; j++)
{
if(maps[i][j] > maps[i][k] + maps[k][j])
maps[i][j] = maps[i][k] + maps[k][j];
}
}
}
return maps[][n];
} int main()
{
int m, i, a, b, c;
while(scanf("%d%d", &n, &m), m + n)
{
Init();
while(m--)
{
scanf("%d%d%d", &a, &b, &c);
G[b][a] = G[a][b] = c;
}
int ans = ;
for(i = ; i <= n - ; i++)
ans = max(ans, Floyd(i));
if(ans == INF)
printf("Inf\n");
else
printf("%d\n", ans);
}
return ;
}

UVALive 7079 - How Many Maos Does the Guanxi Worth(最短路Floyd)的更多相关文章

  1. hdu 5137 How Many Maos Does the Guanxi Worth 最短路 spfa

    How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/5 ...

  2. HDU 5137 How Many Maos Does the Guanxi Worth 最短路 dijkstra

    How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/5 ...

  3. HDU 5137 How Many Maos Does the Guanxi Worth

    How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/5120 ...

  4. hdoj 5137 How Many Maos Does the Guanxi Worth【最短路枚举+删边】

    How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/5 ...

  5. HDU5137 How Many Maos Does the Guanxi Worth(枚举+dijkstra)

    How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/5 ...

  6. How Many Maos Does the Guanxi Worth

    How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/5 ...

  7. (hdoj 5137 floyd)How Many Maos Does the Guanxi Worth

    How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/5 ...

  8. 杭电5137How Many Maos Does the Guanxi Worth

    How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/5 ...

  9. ACM学习历程——HDU5137 How Many Maos Does the Guanxi Worth(14广州10题)(单源最短路)

    Problem Description    "Guanxi" is a very important word in Chinese. It kind of means &quo ...

随机推荐

  1. R语言randomForest包实现随机森林——iris数据集和kyphosis数据集

    library(randomForest)model.forest<-randomForest(Species~.,data=iris)pre.forest<-predict(model. ...

  2. UVa 10635 (LIS+二分) Prince and Princess

    题目的本意是求LCS,但由于每个序列的元素各不相同,所以将A序列重新编号{1,2,,,p+1},将B序列重新编号,分别为B中的元素在A中对应出现的位置(没有的话就是0). 在样例中就是A = {1 7 ...

  3. 51nod1202 子序列个数

    看到a[i]<=100000觉得应该从这个方面搞.如果a[x]没出现过,f[x]=f[x-1]*2;否则f[x]=f[x-1]*2-f[pos[a[x]]-1];ans=f[n]-1,然后WA了 ...

  4. php开启curl扩展

    配置方法: 1.拷贝PHP目录中的libeay32.dll 和 ssleay32.dll 两个文件到 system32 目录. 2.修改php.ini:配置好 extension_dir ,去掉 ex ...

  5. cookie随便写的一点笔记(抄书的)

    cookie是保存在客户端的文本,能够在一定程度上提高用户体验.Servlet API 中提供了Cookie类,可以创建Cookie对象,并通过响应中的addCookie方法,将cookie保存到客户 ...

  6. RNN 与 LSTM 的应用

    之前已经介绍过关于 Recurrent Neural Nnetwork 与 Long Short-Trem Memory 的网络结构与参数求解算法( 递归神经网络(Recurrent Neural N ...

  7. 【自动化测试】PO思路

    http://blog.csdn.net/liubofengpython/article/details/7720078

  8. Java条件语句之 switch

    当需要对选项进行等值判断时,使用 switch 语句更加简洁明了.例如:根据考试的名次,给予前 4 名不同的奖品.第一名,奖励笔记本一台:第二名,奖励 IPAD 2 一个:第三名,奖励移动电源一个:最 ...

  9. K2 Blackpearl开发技术要点(Part1)

    转:http://www.cnblogs.com/dannyli/archive/2012/09/14/2685260.html K2 Blackpearl开发技术要点(Part1) 预知后事如何,请 ...

  10. hdu 4876(剪枝+暴力)

    题意:给定n,k,l,接下来给出n个数,让你从n个数中选取k个数围成一圈,然后从这k个数中随意选出连续的m(m>=1&&m<=k)个数进行异或后得到[l,r]区间的所有值, ...