首页 > 代码库 > hdu 5137 最短路最大化

hdu 5137 最短路最大化

http://acm.hdu.edu.cn/showproblem.php?pid=5137

RT

n只有30,使用spfa遍历每种去点方法找到最大值即可

<span style="font-size:14px;">#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
#define eps 1e-9
const double pi = acos(-1.0);
typedef long long LL;
const int inf = 1000000000;
const int maxn = 1005;
struct edge{
    int v,next,c;
}e[maxn<<1];
int n,m,head[40],ecnt;
bool vis[40];
void add(int u,int v,int c)
{
    e[ecnt] = (edge){v,head[u],c};
    head[u] = ecnt++;
    e[ecnt] = (edge){u,head[v],c};
    head[v] = ecnt++;
}

int dis[40];
void spfa()
{
    for(int i = 1;i <= n;++i)
        dis[i] = inf;
    dis[1] = 0;
    clr0(vis);
    queue<int> q;
    q.push(1);vis[1] = 1;
    while(!q.empty()){
        int u = q.front();q.pop();
        vis[u] = false;
        for(int i = head[u];i != -1;i = e[i].next){
            int v = e[i].v;
            if(dis[v] > dis[u] + e[i].c){
                dis[v] = dis[u] + e[i].c;
                if(!vis[v]){
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
}
int u[maxn],v[maxn],c[maxn],mx = 0;
int mp[40][40];
void work()
{
    clr0(mp);
    mx = 0;
    for(int i = 0;i < m;++i){
        RD3(u[i],v[i],c[i]);
        mp[u[i]][v[i]] = mp[u[i]][v[i]] = c[i];
    }
    for(int i = 2;i < n;++i){
        clr1(head);
        ecnt = 0;
        for(int j = 0;j < m;++j){
            if(i == u[j] || i == v[j])
                continue;
            add(u[j],v[j],c[j]);
        }
//        for(int j = 1;j <= n;++j)
//            for(int k = 1;k <= n;++k){
//                if(i == j || i == k || !mp[j][k])
//                    continue;
//                add(j,k,mp[j][k]);
//            }
        spfa();
        mx = max(mx,dis[n]);
    }
    if(mx == inf)
        puts("Inf");
    else
        printf("%d\n",mx);
}
int main()
{
//    int _,cas = 1;
//    RD(_);
//    while(_--){
//        printf("Case #%d: %I64d\n",cas++,work());
//    }
    while(RD2(n,m),n|m){
        work();
    }
    return 0;
}
/*

*/
</span>


hdu 5137 最短路最大化