zoukankan      html  css  js  c++  java
  • 巨水的题目

    //  今天翻了翻以前收藏的东西,居然发现

    :  spfa  用map  写的 ,好吧,stl  我一无所知。

    题目:hdu  2544   太熟悉了吧

    #include<stdio.h>
    #include <map>
    #include <queue>
    #define N 101
    using namespace std;
    
    map<int,int>  head[N];// head[u][v]=w
    int  spfa(int st,int n)  //start
    {
        int i,u,v,w;
        int vis[N],d[N];
        for (i=1;i<=n;i++)
        {vis[i]=0;d[i]=0x7ffffff;}
        vis[st]=1; d[st]=0;
    
        queue<int> q; q.push(st);
        while(!q.empty())
        {
            u=q.front();
            vis[u]=0; q.pop();
            for (map<int,int>::iterator it=head[u].begin();it!=head[u].end();it++)
            {
                v=it->first;  w=it->second;
                if (d[u]+w<d[v])
                {
                    d[v]=d[u]+w;
                    q.push(v);vis[v]=1;
                }
            }
        }
        return d[n];
    }
    int main()
    {
        int n,m;
    
    
        while(scanf("%d%d",&n,&m),n+m)
        {
            int i,u,v,w;
            for (i=0;i<m;i++)
            {
                scanf("%d%d%d",&u,&v,&w);
                head[u][v]=head[v][u]=w;
            }
            printf("%d\n", spfa(1,n));
            for (i=0;i<n;i++)  head[i].clear();
        }
        return 0;
    }
    Just a little, maybe change the world
  • 相关阅读:
    P1363-幻象迷宫
    P1582-倒水
    P2123-皇后游戏
    P1233-木棍加工
    P1052-过河
    P1541-乌龟棋
    P1736-创意吃鱼法
    P1417-烹调方案
    LeetCode--043--字符串相乘(java)
    LeetCode--041--缺失的第一个整数(java)
  • 原文地址:https://www.cnblogs.com/skyming/p/2479608.html
Copyright © 2011-2022 走看看