zoukankan      html  css  js  c++  java
  • hdu 1535 Invitation Cards

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int inf=1<<24;
    const int N=1000000+5;
    
    struct node
    {
        int to,w;
        node* next;
    };
    node* edge[N];
    node* reedge[N];
    int n,m,x,dist[N],inq[N],q[N];
    
    void spfa(int flag)
    {
        int i,u,h=0,t=1;
        node* ptr;
        for(i=0; i<=n; i++)
        {
            dist[i]=inf;
            inq[i]=0;
        }
        dist[1]=0;
        inq[1]=1;
        q[0]=1;
        while(h!=t)
        {
            u=q[h];
            h++;
            inq[u]=0;
            if(flag==1) ptr=edge[u];
            else ptr=reedge[u];
            while(ptr)
            {
                if(dist[ptr->to]>(dist[u]+ptr->w))
                {
                    dist[ptr->to]=dist[u]+ptr->w;
                    if(inq[ptr->to]==0)
                    {
                        q[t]=ptr->to;
                        t++;
                        inq[ptr->to]=1;
                    }
                }
                ptr=ptr->next;
            }
    
        }
    }
    
    int main()
    {
        int cas,i,u,v,w,ans;
        scanf("%d",&cas);
        while(cas--)
        {
            scanf("%d%d",&n,&m);
            for(i=1; i<=n; i++)
            {
                edge[i]=NULL;
                reedge[i]=NULL;
            }
            for(i=0; i<m; i++)
            {
                scanf("%d%d%d",&v,&u,&w);
    
                node *temp;
                temp= new node;
                temp->to=u;
                temp->w=w;
                temp->next=NULL;
                if(edge[v]==NULL)
                {
                    edge[v]=temp;
                }
                else
                {
                    temp->next=edge[v];
                    edge[v]=temp;
                }
    
                temp= new node;
                temp->to=v;
                temp->w=w;
                temp->next=NULL;
                if(reedge[u]==NULL)
                {
                    reedge[u]=temp;
                }
                else
                {
                    temp->next=reedge[u];
                    reedge[u]=temp;
                }
            }
            ans=0;
            spfa(1);
            for(i=2; i<=n; i++)
            {
                //printf("%d
    ",dist[i]);
                ans+=dist[i];
            }
            spfa(0);
            for(i=2; i<=n; i++)
            {
                ans+=dist[i];
            }
            printf("%d
    ",ans);
        }
        return 0;
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

  • 相关阅读:
    敏捷开发方法综述
    RBAC权限控制系统
    Thinkphp 视图模型
    Thinkphp 缓存和静态缓存局部缓存设置
    Thinkphp路由使用
    Thinkphp自定义标签
    异步处理那些事
    Thinkphp 关联模型
    Thinkphp 3.1. 3 ueditor 1.4.3 添加水印
    数据库组合
  • 原文地址:https://www.cnblogs.com/xryz/p/4847953.html
Copyright © 2011-2022 走看看