标签:
题目大意:给你一个关系图,让你再删除一个点之后(除了1,和n)。让你求出来从1到n的最短路的最大值,如果不可达输出Inf。
解题思路:题意也说了,就是暴力枚举点,然后跑n-2次spfa求出来一个最大值就可以了。
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
50 Inf
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 1000000007
#define Read() freopen("autocomplete.in","r",stdin)
#define Write() freopen("autocomplete.out","w",stdout)
#define Cin() ios::sync_with_stdio(false)
using namespace std;
inline int read()
{
char ch;
bool flag = false;
int a = 0;
while(!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')));
if(ch != '-')
{
a *= 10;
a += ch - '0';
}
else
{
flag = true;
}
while(((ch = getchar()) >= '0') && (ch <= '9'))
{
a *= 10;
a += ch - '0';
}
if(flag)
{
a = -a;
}
return a;
}
void write(int a)
{
if(a < 0)
{
putchar('-');
a = -a;
}
if(a >= 10)
{
write(a / 10);
}
putchar(a % 10 + '0');
}
const int maxn = 110;
int vis[maxn];
int dis[maxn];
vector<int>f[maxn];
int mp[maxn][maxn];
int bfs(int x, int n)
{
memset(dis, INF, sizeof(dis));
memset(vis, 0, sizeof(vis));
queue<int>que;
dis[1] = 0;
vis[1] = 1;
que.push(1);
while(!que.empty())
{
int xp = que.front();
que.pop();
vis[xp] = 0;
int sx = f[xp].size();
for(int i = 0; i < sx; i++)
{
int u = f[xp][i];
if(u == x) continue;
if(dis[u] > dis[xp]+mp[xp][u])
{
dis[u] = dis[xp]+mp[xp][u];
if(vis[u]) continue;
vis[u] = 1;
que.push(u);
}
}
}
return dis[n];
}
int main()
{
int n, m;
while(~scanf("%d %d",&n, &m))
{
if(!n && !m) break;
memset(mp, 0, sizeof(mp));
for(int i = 1; i <= n; i++) f[i].clear();
int x, y, w;
for(int i = 0; i < m; i++)
{
cin >>x>>y>>w;
mp[x][y] = mp[y][x] = w;
f[x].push_back(y);
f[y].push_back(x);
}
int Max = 0;
for(int i = 2; i < n; i++)
{
Max = max(Max, bfs(i, n));
}
if(Max == INF) cout<<"Inf"<<endl;
else cout<<Max<<endl;
}
return 0;
}
HDU 5137 How Many Maos Does the Guanxi Worth(暴力+spfa)
标签:
原文地址:http://blog.csdn.net/xu12110501127/article/details/42921761