hdu 5137 去掉一个点 使得最短路最大(2014广州区域赛K题)

题意:从2~n-1这几个点中任意去掉一个点,使得从1到n的最短路径最大,如果任意去掉一个点1~n无通路输出Inf。

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

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <string>
 6 # include <map>
 7 # include <cmath>
 8 # include <queue>
 9 # include <list>
10 # define LL long long
11 using namespace std ;
12
13 const int MAXN=300;
14 const int INF=0x3f3f3f3f;
15 int n  , m ;
16 bool vis[MAXN];
17 int cost[MAXN][MAXN] ;
18 int lowcost[MAXN] ;
19
20 void Dijkstra(int beg)
21 {
22     for(int i=0;i<n;i++)
23     {
24         lowcost[i]=INF;
25     }
26     lowcost[beg]=0;
27     for(int j=0;j<n;j++)
28     {
29         int k=-1;
30         int Min=INF;
31         for(int i=0;i<n;i++)
32             if(!vis[i]&&lowcost[i]<Min)
33             {
34                 Min=lowcost[i];
35                 k=i;
36             }
37             if(k==-1)
38                 break ;
39             vis[k]=true;
40             for(int i=0;i<n;i++)
41                 if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
42                 {
43                     lowcost[i]=lowcost[k]+cost[k][i];
44                 }
45     }
46
47 }
48
49 int main ()
50 {
51     //freopen("in.txt","r",stdin) ;
52     while (scanf("%d %d" , &n , &m) !=EOF)
53     {
54         if (n == 0 && m == 0)
55             break ;
56
57         int u , v , w ;
58         int i , j ;
59         int MAX = 0 ;
60         for (i = 0 ; i < n ; i++)
61         for (j = 0 ; j < n ; j++)
62        {
63            if (i == j)
64               cost[i][j] = 0 ;
65            else
66               cost[i][j] = INF ;
67        }
68         while(m--)
69         {
70             scanf("%d%d%d" , &u , &v , &w) ;
71             cost[u-1][v-1] = w ;
72             cost[v-1][u-1] = w ;
73
74         }
75
76         for (i = 1 ; i <= n-2 ; i++)
77         {
78             memset(vis , 0 , sizeof(vis)) ;
79             vis[i] = 1 ;
80             Dijkstra(0) ;
81             if (lowcost[n-1] > MAX )
82                 MAX = lowcost[n-1] ;
83
84         }
85
86         if (MAX == INF)
87            printf("Inf\n") ;
88         else
89            printf("%d\n" , MAX) ;
90     }
91
92     return 0 ;
93 }

时间: 2024-10-19 10:47:28

hdu 5137 去掉一个点 使得最短路最大(2014广州区域赛K题)的相关文章

hdu 5122 (2014北京区域赛 K题)

把一个序列按从小到大排序 要执行多少次操作 只需要从右往左统计,并且不断更新最小值,若当前数为最小值,则将最小值更新为当前数,否则sum+1 Sample Input255 4 3 2 155 1 2 3 4 Sample OutputCase #1: 4Case #2: 1 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5

hdu 4463 有一条边必须加上 (2012杭州区域赛K题)

耐克店 和 苹果店必须相连 Sample Input42 30 01 00 -1 1 -10 Sample Output3.41 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # define LL long long 7 using namespace std ; 8

hdu 5112 (2014北京区域赛 A题)

给出某个时刻对应的速度 求出相邻时刻的平均速度 输出最大值 Sample Input23 // n2 2 //t v1 13 430 31 52 0 Sample OutputCase #1: 2.00Case #2: 5.00 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <string>

HDU 5024 Wang Xifeng&#39;s Little Plot(2014广州网络赛1003)

写了1h的DFS,简直被自己的代码吓哭了..不过起码还是思路清晰,QUQ~ 说一下题意吧: 题意是求一条最长路,最多能经过一次转弯,并且其角度只能为90度. 拿第一个样例来说:(0,1)->(1,2)->[转弯](2,1) ,所以答案是3. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5024 代码如下: #include<iostream> #include<cstdio> #include<cstring>

HDU 5131 Little Zu Chongzhi&#39;s Triangles (状压DP +2014广州现场赛)

题目链接:HDU 5131 Little Zu Chongzhi's Triangles 题意:给出一些线段,在其中选出3根组成三角形,问用这些线段组成的所有三角形的最大面积是多少. 7 3 4 5 3 4 5 90 两个三角形是(3,3,4),(5,5,4). 思路:N最大12,状态压缩,把所有可能组成的三角形存起来.A&B==0则说明A|B状态是有效的. 贪心也能过..为什么? AC代码: #include <stdio.h> #include <string.h> #

HDU 5131 Song Jiang&#39;s rank list (结构体+MAP,2014广州现场赛)

题目链接:HDU 5131 Song Jiang's rank list 题意:对给出的好汉按杀敌数从大到小排序,若相等,按字典序排.M个询问,询问名字输出对应的主排名和次排名.(排序之后)主排名是在该名字前比他杀敌数多的人的个数加1,次排名是该名字前和他杀敌数相等的人的个数加1,(也就是杀敌数相等,但是字典序比他小的人数加1). AC代码: #include <stdio.h> #include <string> #include <map> #include <

HDU 5024 Wang Xifeng&#39;s Little Plot(广州网络赛C题)

HDU 5024 Wang Xifeng's Little Plot 题目链接 思路:先利用记忆化搜索预处理出每个结点对应8个方向最远能走多远,然后枚举拐点记录最大值即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int d[8][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 1}, {1,

[ACM] HDU 5137 How Many Maos Does the Guanxi Worth(去掉一个点使得最短路最大化)

How Many Maos Does the Guanxi Worth 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

HDU 5137 How Many Maos Does the Guanxi Worth (14广州 Floyd 最短路)

How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 468    Accepted Submission(s): 164 Problem Description "Guanxi" is a very important word in Chinese. I