zoukankan      html  css  js  c++  java
  • HDU 4460

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

    水题一道,oj时间卡的非常奇怪,把spfa的queue开成全局卡线过,别的全挂了,迪杰斯特拉的手写堆都超时,可能是卡了map?

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <map> 
    #include <queue>
    using namespace std ;
    
    const int INF=0xfffffff ;
    struct Edge
    {
        int s,t,v,nxt ;
    }e[20005] ;
    int n,m,cnt,head[1005],dis[1005],vis[1005] ;
    void add(int s,int t,int v)
    {
        e[cnt].s=s ;e[cnt].t=t ;e[cnt].v=v ;e[cnt].nxt=head[s] ;head[s]=cnt++ ;
    }
    queue <int> q ;
    void spfa(int s)
    {
        for(int i=1 ;i<=n ;i++)
            dis[i]=INF ;
        dis[s]=0 ;
        memset(vis,0,sizeof(vis)) ;
        vis[s]=1 ;
        q.push(s) ;
        while(!q.empty())
        {
            int u=q.front() ;
            q.pop() ;
            vis[u]=0 ;
            for(int i=head[u] ;i!=-1 ;i=e[i].nxt)
            {
                int tt=e[i].t ;
                if(dis[tt]>dis[u]+e[i].v)
                {
                    dis[tt]=dis[u]+e[i].v ;
                    if(!vis[tt])
                    {
                        vis[tt]=1 ;
                        q.push(tt) ;
                    }
                }
            }
        }
    }
    int main()
    {
        while(cin >> n)
        {
            if(!n)break ;
            cnt=0 ;
            memset(head,-1,sizeof(head)) ;
            map <string,int> mp ;
            for(int i=0 ;i<n ;i++)
            {
                string na ;
                cin >> na ;
                mp[na]=i+1 ;
            }
            cin >> m ;
            while(m--)
            {
                string a,b ;
                cin >> a >> b ;
                add(mp[a],mp[b],1) ;
                add(mp[b],mp[a],1) ;
            }
            int ans=0 ;
            for(int i=1 ;i<=n ;i++)
            {
                spfa(i) ;
                for(int j=1 ;j<=n ;j++)
                {
                    ans=max(ans,dis[j]) ;
                }
            }
            if(ans==INF)puts("-1") ;
            else printf("%d
    ",ans) ;
        }
        return 0 ;
    }
    View Code
  • 相关阅读:
    ubuntu安装
    学习资料

    disksim-3.0 with flashsim 安装
    STL
    存储引擎
    数据库索引
    数据库表、字段设计
    查询SQL优化
    导航栏实现
  • 原文地址:https://www.cnblogs.com/xiaohongmao/p/3957197.html
Copyright © 2011-2022 走看看