zoukankan      html  css  js  c++  java
  • uva 10986 Sending email

    最短路裸题啊,顶点数较多开不了邻接矩阵的,而且边数相对较少,稀疏图,用邻接表,写了个spfa和一个优先队列的dij,当做练手

    spfa

    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    #define N 20010
    #define M 50010*2
    #define INF 0x3f3f3f3f
    struct edge
    { int u,v,w,next; }e[M];
    int first[N],d[N],vis[N];
    int n,m,s,t;
    
    void spfa()
    {
        queue <int> q;
        memset(d,0x3f,sizeof(d));
        memset(vis,0,sizeof(vis));
        d[s]=0; q.push(s); vis[s]=1;
        while(!q.empty())
        {
            int u,v,w;
            u=q.front(); q.pop();  vis[u]=0;
            for(int i=first[u]; i!=-1 ; i=e[i].next)
            {
                v=e[i].v;  w=e[i].w;
                if(d[u]+w<d[v])
                {
                    d[v]=d[u]+w;
                    if(!vis[v])
                    { q.push(v); vis[v]=1; }
                }
            }
        }
        return ;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        for(int c=1; c<=T; c++)
        {
            scanf("%d%d%d%d",&n,&m,&s,&t);
            memset(first,-1,sizeof(first));
            m*=2;
            for(int i=0; i<m; i+=2)
            {
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                e[i].u=u; e[i].v=v;  e[i].w=w;
                e[i].next=first[u];
                first[u]=i;
                e[i+1].u=v; e[i+1].v=u; e[i+1].w=w;
                e[i+1].next=first[v];
                first[v]=i+1;
            }
    
            spfa();
            printf("Case #%d: ",c);
            if(d[t]!=INF)  printf("%d\n",d[t]);
            else           printf("unreachable\n");
        }
        return 0;
    }

    dij优先队列

    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <utility>
    using namespace std;
    #define N 20010
    #define M 50010*2
    #define INF 0x3f3f3f3f
    struct edge
    { int u,v,w,next; }e[M];
    int first[N],d[N],done[N];
    int n,m,s,t;
    
    void dij()
    {
        //最好用个 typedef pair<int,int> pii;
        priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
        memset(d,0x3f,sizeof(d));
        memset(done,0,sizeof(done));
        d[s]=0;
        q.push( make_pair(d[s],s) );
        while(!q.empty())
        {
            pair<int,int> x;  int u;
            x=q.top(); q.pop(); u=x.second;
            if(done[u]) continue;
            done[u]=1;
            for(int i=first[u]; i!=-1; i=e[i].next)
            {
                int v,w;
                v=e[i].v;  w=e[i].w;
                if(d[u]+w<d[v])
                {
                    d[v]=d[u]+w;
                    q.push(make_pair(d[v],v));
                }
            }
        }
        return ;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        for(int c=1; c<=T; c++)
        {
            scanf("%d%d%d%d",&n,&m,&s,&t);
            memset(first,-1,sizeof(first));
            m*=2;
            for(int i=0; i<m; i+=2)
            {
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                e[i].u=u; e[i].v=v;  e[i].w=w;
                e[i].next=first[u];
                first[u]=i;
                e[i+1].u=v; e[i+1].v=u; e[i+1].w=w;
                e[i+1].next=first[v];
                first[v]=i+1;
            }
    
            dij();
            printf("Case #%d: ",c);
            if(d[t]!=INF)  printf("%d\n",d[t]);
            else           printf("unreachable\n");
        }
        return 0;
    }
  • 相关阅读:
    Android
    列出控制原理来解决
    【ORACLE】spfile失落的处理
    Cocos2d-x 脚本语言Lua基本语法
    远东转载这说明一些有实力的话人工智能协会的思维和潜意识之间移动的一篇文章
    日志框架的实时变化,即日起,思维详细框架(4)
    [Python] Scatter Plot for daily return
    [Python] Use a Python Generator to Crawl the Star Wars API
    [Poi] Setup PostCSS and Tailwind with Poi
    [Poi] Build and Analyze Your JavaScript Bundles with Poi
  • 原文地址:https://www.cnblogs.com/scau20110726/p/2811906.html
Copyright © 2011-2022 走看看