zoukankan      html  css  js  c++  java
  • 杭电5137--How Many Maos Does the Guanxi Worth(Spfa+暴力枚举)

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

    题意:话说题意很坑啊、第一次做这个题暴力枚举也想起来了, 但。。。。 题目说的是在路径中去任意除一个点, 看1~n是否还联通,求权值和最大的最短路的权值和,题目中给的点数最大是30,暴力枚举就出来了。 注意一点: Sp 和 Dij 中是对Map数组处理。

    Spfa: 
    #include <queue> 
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #define max(a, b) a>b?a:b
    using namespace std;
    const int INF = 0x3f3f3f3f;
    int map[33][33], Map[33][33], dis[33], vis[33]; 
    int n, m;
    int Spfa(int a)
    {
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
            {
                if(i == a || j == a)
                    Map[i][j] = INF;
                else 
                    Map[i][j] = map[i][j]; 
            }
        for(int i = 1; i <= n; i++)
            dis[i]=INF; 
        dis[1] = 0; vis[1] = 1;
        queue<int> Q;
        Q.push(1);
        while(!Q.empty()) 
        {
            int temp = Q.front();
            Q.pop();  vis[temp] = 0;
            for(int j = 1; j <= n; j++)
                if(dis[j] > dis[temp] + Map[temp][j])
                {
                    dis[j] = dis[temp] + Map[temp][j]; 
                    if(!vis[j])
                    {
                        vis[j] = 1;
                        Q.push(j);    
                    }    
                } 
        }
        return dis[n];
    } 
    int main()
    {
        while(scanf("%d %d", &n, &m), n+m)
        {
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= n; j++)
                    map[i][j]=(i==j?0:INF);
            int u, v, w;
            for(int i = 1; i <= m; i++)
            {
                scanf("%d %d %d", &u, &v, &w);
                if(map[u][v] > w)
                    map[u][v]=map[v][u]=w;
            }
            int ans = 0;
            for(int i = 2; i < n; i++)     //暴力枚举, 很精髓。 
                ans = max(ans, Spfa(i));
            if(ans == INF)
                printf("Inf
    ");
            else
                printf("%d
    ", ans); 
        }
        return 0;
    } 
    Dijkstra:练练模板。 
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #define max(a, b) a>b?a:b 
    const int INF = 0x3f3f3f3f; 
    using namespace std; 
    int map[33][33], Map[33][33], dis[33], vis[33];
    int n, m;
    int Dijkstra(int a)
    {
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
            {
                if(i == a || j == a)
                    Map[i][j] = INF;
                else
                     Map[i][j] = map[i][j]; 
            }    
        for(int i = 1; i <= n; i++)
            dis[i] = map[1][i];
        vis[1] = 1;
        for(int i = 1; i < n; i++)
        {
            int temp, min = INF;
            for(int j = 1; j <= n; j++)
            {
                if(!vis[j] && dis[j] < min)
                {
                    temp = j;
                    min = dis[j];
                }
            }
            vis[temp] = 1; 
            for(int j = 1; j <= n; j++)
                if(!vis[j] && dis[j] > dis[temp] + Map[temp][j])
                    dis[j] = dis[temp] + Map[temp][j];
        }
        return dis[n];
    } 
    int main()
    {
        while(scanf("%d %d", &n, &m), m+n)
        {
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= n; j++)
                    map[i][j]=(i==j?0:INF);
            int u, v, w;
            for(int i = 1; i <= m; i++)
            {
                scanf("%d %d %d", &u, &v, &w);
                if(map[u][v] > w)
                    map[u][v] = map[v][u] = w;
            }
            int maxx = 0;
            for(int i = 2; i < n; i++)
                maxx = max(maxx, Dijkstra(i));
            if(maxx == INF)
                printf("Inf
    ");
            else
                printf("%d
    ", maxx);
        }
        return 0;    
    } 
  • 相关阅读:
    Linux05——用户操作
    租房子-----多选题
    查询
    增删
    PHP基础
    数据库--高级查询
    CRUD查询
    CRUD操作
    数据库
    轮播
  • 原文地址:https://www.cnblogs.com/soTired/p/4754874.html
Copyright © 2011-2022 走看看