现在的位置: 首页 > 综合 > 正文

HDU 2680 Choose the best route(单源最短路径)

2013年10月20日 ⁄ 综合 ⁄ 共 2893字 ⁄ 字号 评论关闭

写了各种版本,但由于进队列的括号没加,各种爆内存的飘过。╮(╯▽╰)╭

1.djkstra+邻接矩阵

8300966 2013-05-14 18:12:47 Accepted 2680 375MS 4156K 1274 B G++

加个超级源点水之。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define inf 99999999
const int maxn=1000+5;
int map[maxn][maxn];
int dist[maxn],n,s;
bool vis[maxn];
void djkstra()
{
    memset(vis,0,sizeof(vis));
    for(int i=0;i<=n;i++) dist[i]=map[0][i];
    vis[0]=1;dist[0]=0;
    for(int t=0;t<n;t++){
        int mindis=inf,idx=-1;
        for(int i=1;i<=n;i++)
            if(!vis[i]&&mindis>dist[i])
                mindis=dist[i],idx=i;
        if(idx==-1||idx==s) break;
        vis[idx]=1;
        for(int i=1;i<=n;i++)
            if(!vis[i]&&dist[i]>mindis+map[idx][i])
                dist[i]=map[idx][i]+mindis;
    }
}
int main()
{
    int m;
    while(~scanf("%d%d%d",&n,&m,&s))
    {
        for(int i=0;i<=n;i++)
            for(int j=0;j<=n;j++)
                map[i][j]=inf;
        while(m--)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            map[u][v]=min(map[u][v],w);
        }
        int w;
        scanf("%d",&w);
        while(w--)
        {
            int u;
            scanf("%d",&u);
            map[0][u]=0;
        }
        djkstra();
        if(dist[s]==inf) printf("-1\n");
        else printf("%d\n",dist[s]);
    }
    return 0;
}

2.SPFA+vector二维数组实现

Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
8303378 2013-05-14 22:10:24 Accepted 2680 281MS 960K 1321 B G++

#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define inf 99999999
const int maxn=1000+5;
struct Node
{
    int to,w;
};
int n,s;
int dist[maxn],w[maxn];
bool mark[maxn];
vector<Node>map[maxn];
void SPFA(int u)
{
    for(int i=1; i<=n; i++) dist[i]=inf,mark[i]=false;
    queue<int>q;
    q.push(u);
    dist[u]=0;
    mark[u]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        mark[u]=0;
        for(int i=0; i<map[u].size(); i++)
        {
            int to=map[u][i].to;
            int w=map[u][i].w;
            if(dist[to]>dist[u]+w)
            {
                dist[to]=dist[u]+w;
                if(!mark[to])
                {
                    q.push(to);
                    mark[to]=1;
                }
            }
        }
    }
}
int main()
{
    int m;
    while(~scanf("%d%d%d",&n,&m,&s))
    {
        for(int i=1; i<=n; i++) map[i].clear();
        while(m--)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            Node p;
            p.to=u,p.w=w;
            map[v].push_back(p);
        }
        SPFA(s);
        int Mindis=inf,w,x;
        scanf("%d",&w);
        while(w--)
        {
            scanf("%d",&x);
            Mindis=min(Mindis,dist[x]);
        }
        if(Mindis==inf) printf("-1\n");
        else  printf("%d\n",Mindis);
    }
    return 0;
}

3.SPFA+邻接表实现

8303347 2013-05-14 22:06:08 Accepted 2680 281MS 500K 1584 B G++

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
#define inf 99999999
const int maxn=1000+5;
struct Node
{
    int to,w,next;
} edge[maxn*20];
int n,s,cnt;
bool mark[maxn];
int dist[maxn],head[maxn];
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
    edge[cnt].to=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void SPFA(int u)
{
    for(int i=1; i<=n; i++) dist[i]=inf,mark[i]=false;
    queue<int>q;
    q.push(u);
    mark[u]=true;
    dist[u]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        mark[u]=false;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int to=edge[i].to;
            int w=edge[i].w;
            if(dist[to]>dist[u]+w)
            {
                dist[to]=dist[u]+w;
                if(!mark[to])
                {
                    q.push(to);
                    mark[to]=true;
                }
            }
        }
    }
}
int main()
{
    int m;
    while(~scanf("%d%d%d",&n,&m,&s))
    {
        init();
        while(m--)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(v,u,w);
        }
        SPFA(s);
        int Mindis=inf,w,x;
        scanf("%d",&w);
        while(w--)
        {
            scanf("%d",&x);
            Mindis=min(Mindis,dist[x]);
        }
        if(Mindis==inf) printf("-1\n");
        else  printf("%d\n",Mindis);
    }
    return 0;
}

抱歉!评论已关闭.