hdu5137How Many Maos Does the Guanxi Worth

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std ;
const int maxn = 1010 ;
typedef long long ll ;
const ll inf = 1e18 ;
struct Edge
{
    int v , next ;
    ll w ;
}edge[maxn<<1] ;
int head[maxn] , vis[maxn] ;
ll dis[maxn] ;
int nedge ;
void addedge(int u , int v,  int w)
{
    edge[nedge].v = v ;
    edge[nedge].next = head[u] ;
    edge[nedge].w = w  ;
    head[u] = nedge++ ;
}
int n , m ;
ll spfa(int x)
{
    queue<int> que ;
    for(int i = 2;i <= n;i++)
    dis[i] = inf ;
    memset(vis , 0 , sizeof(vis))  ;
    dis[1] = 0 ;
    vis[1] = 1  ;
    que.push(1) ;
    while(que.size())
    {
        int u = que.front() ; que.pop() ;
        vis[u] = 0 ;
        for(int i = head[u]; i != -1 ; i = edge[i].next)
        {
            int v = edge[i].v ;
            if(v == x)continue ;
            if(dis[u] + edge[i].w < dis[v])
            {
                dis[v] = dis[u] + edge[i].w ;
                if(!vis[v])
                {
                    vis[v] = 1 ;
                    que.push(v) ;
                }
            }
        }
    }
    return dis[n] ;
}
int main()
{
    while(~scanf("%d%d", &n , &m) && (n+m))
    {
        memset(head , -1 , sizeof(head)) ;
        nedge = 0 ;
        while(m--)
        {
            int  u , v , w  ;
            scanf("%d%d%d" , &u , &v , &w) ;
            addedge(u , v , w) ;
            addedge(v , u , w) ; 
        }
        ll ans = 0 ;
        for(int i = 2;i < n;i++)
        ans = max(ans , spfa(i)) ;
        if(ans == inf)
        puts("Inf") ;
        else
        cout<<ans<<endl;
    }
}

你可能感兴趣的:(hdu5137How Many Maos Does the Guanxi Worth)