码迷,mamicode.com
首页 > 其他好文 > 详细

hrbust 1339 Touring 最短路Dijkstra 邻接表

时间:2015-08-02 13:47:31      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:算法   c++   dijkstra   优先队列   hrbust   

题目描述:

两个人从同一出发点去不同的地方,路的距离会造成花费的多少,所以两个人走的越短越好,并且两个人同乘一辆车可以使花费更低,给出每条路所连接的两个城市及该线路的花费以及两个人的出发点和他们各自的目的地,求他们需要的最小花费是多少

分析:
先同乘一段距离,再分开走,形走路线是一个Y形路线,找出一个点,使他到出发点以及他们各自的目的地三个地方的最短距离的和最小,这三个距离的和就是他们所需的最小距花费



这道题换了三种方法打的,第一种是邻接矩阵 乱入 优先队列,超空间,w[i][j]来储存 i 地到 j 地 的花费 ,后来发现若用邻接矩阵来储存花费,必定超时

超空间代码。。:其实那个spfa函数并不是spfa算法,名字是我乱入的。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=1010;
const int INF=0x1f1f1f1f;
const int INF1=1<<25-1;
int dislena[maxn];
int dislenb[maxn];
int dislenc[maxn];
int w[maxn][maxn];
bool visit[maxn];
vector<int> G[maxn];
int n,m;
int s,t;
struct HeapNode
{
   int d,u;
   bool operator<(const HeapNode & rhs)const
    {
   return d>rhs.d;
   }
};
void spfa(int s,int * dislen)
{
     int i,j;
         for(i=1; i<=n; i++)
            {
                if(i==s)
                {
                    dislen[i]=0;
                }
                else
                {
                    dislen[i]=w[s][i];
                }
            }
    priority_queue <HeapNode> Q;
    memset(visit,0,sizeof(visit));
    HeapNode tt;
    tt.d=0;
    tt.u=s;
      Q.push(tt);
    while(!Q.empty())
    {
        HeapNode x=Q.top();
        Q.pop();
        int u=x.u;
        if(visit[u])
            continue;
        visit[u]=true;
        for(i=0;i<G[u].size();i++)
        {
                   int v=G[u][i];
                    if(visit[v]==false&&dislen[v]>=dislen[u]+w[u][v])
                    {
                        dislen[v]=dislen[u]+w[u][v];
                        HeapNode t;
                        t.d=dislen[v];
                        t.u=v;
                        Q.push(t);
                       // Q.push((HeapNode){dislen[v],v});
                    }
        }
    }
}
int main()
{
    int cnt=1;
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%d",&m);
        if(n==0&&m==0)
            break;
        int i,j;
       memset(w,0x1f,sizeof(w));
        for(i=0;i<=n;i++)
        {
            G[i].clear();
        }
        int c,a,b;
        scanf("%d%d%d",&c,&a,&b);
        int u,v,len;
        //cout<<"m==  "<<m<<endl;
        for(i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&len);
           if(w[u][v]>=len)
            {
                w[u][v]=len;
                w[v][u]=len;
                G[u].push_back(v);
                G[v].push_back(u);
            }
        }
        //cout<<"this is a good end"<<endl;
            spfa(c,dislenc);
            spfa(a,dislena);
            spfa(b,dislenb);
            cout<<"Scenario #"<<cnt++<<endl;
            if(dislenc[b]==INF||dislenc[a]==INF)
            {
                cout<<"Can not reah!"<<endl<<endl;
                continue;
            }
            long long  res=INF1*3;
            for(i=1;i<=n;i++)
            {
             long long k=dislena[i]+dislenb[i]+dislenc[i];
               if(k<res)
                    res=k;
            }
            cout<<res<<endl<<endl;
    }
    return 0;
}
第二种:已AC

正宗的邻接表,head[u]数组用来储存 u 这个点与其相连的边的编号,Arc数组里保存了相邻的点v和dis[u][v],以及下一个与u相邻的点v1所在的边的编号, 这样可以找到dis[u][v1],当。next_arc==-1时,点u相邻的点已经全部找完。碰到一个问题,不知道为什么,dij函数里加k变量的话就ac,不加的话就超时;

代码:

//m远小于n^2,故邻接矩阵超时,时间复杂度n^2,适用于稠密图,而当m远小于n^2时,可用于邻接表
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=20010;
const int INF=0x1f1f1f1f;
const int INF1=1<<25-1;
int dislena[5010];
int dislenb[5010];
int dislenc[5010];
bool visit[5010];
int n,m;
int s,t;
struct Str
{
    int num;
    int cost;
    Str(int a,int b):num(a),cost(b){}
    bool operator <(const  Str & res)const
    {
        return cost>res.cost;
    }
};
struct Arc
{
    int next_arc;
    int point;
    int cost;
};
Arc arc[maxn];
int head[5010];
void dij(int src,int n,int *low)
{
    memset(visit,0,sizeof(visit));
    priority_queue<Str> q;
    Str str1(src,0);
    q.push(str1);
    int k=0;
    while(k<n&&!q.empty())
    {
        Str s=q.top();
        q.pop();
        if(visit[s.num]==1)
            continue;
            visit[s.num]=1;
        low[s.num]=s.cost;
        k++;
        for(int e=head[s.num];e!=-1;e=arc[e].next_arc)
        {
            if(visit[arc[e].point]==0)
            {
                Str str2(arc[e].point,arc[e].cost+s.cost);
                q.push(str2);
            }
        }
    }
}
int main()
{
    int cnt=1;
    while(scanf("%d",&n)!=EOF)
    {
        memset(head,-1,sizeof(head));
        memset(dislena,0x1f,sizeof(dislena));
        memset(dislenb,0x1f,sizeof(dislenb));
        memset(dislenc,0x1f,sizeof(dislenc));
        scanf("%d",&m);
        if(n==0&&m==0)
            break;
        int i,j;
        int c,a,b;
        scanf("%d%d%d",&c,&a,&b);
        int u,v,len;

        for(i=1; i<=m; i++)
        {
            scanf("%d%d%d",&u,&v,&len);
          arc[i].next_arc=head[u];
          arc[i].point=v;
          arc[i].cost=len;
          head[u]=i;


          arc[m+i].next_arc=head[v];
          arc[m+i].point=u;
          arc[m+i].cost=len;
          head[v]=m+i;
        }
           dij(c,n,dislenc);
           dij(a,n,dislena);
           dij(b,n,dislenb);
            printf("Scenario #%d\n",cnt++);
            if(dislenc[b]==INF||dislenc[a]==INF)
            {
                printf("Can not reah!\n");
                continue;
            }
            long long  res=INF1*3;
            for(i=1;i<=n;i++)
            {
             long long k=dislena[i]+dislenb[i]+dislenc[i];
               if(k<res)
                    res=k;
            }
            printf("%d\n\n",res);
    }
    return 0;
}

第三种:

用G[u][i]表示u点相连的点v在所在的边edges[G[u][i]],其实与第二种很像;

代码:已AC

//m远小于n^2,故邻接矩阵超时,时间复杂度n^2,适用于稠密图,而当m远小于n^2时,可用于邻接表
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x1f1f1f1f;
const int INF1=1<<25-1;
int dislena[5010];
int dislenb[5010];
int dislenc[5010];
int n,m;
struct Edge
{
    int from;
    int to;
    int dist;
    Edge(int u,int v,int d):from(u),to(v),dist(d) {}
};
vector<Edge> edges;
vector<int > G[5010];
bool visit[5010];
void init()
{
    for(int i=0; i<=n; i++)
    {
        G[i].clear();
    }
      edges.clear();
}
void AddEdge(int from,int to,int dist)
{
    edges.push_back(Edge(from,to,dist));
    int m=edges.size();
    G[from].push_back(m-1);
}
struct HeapNode
{
    int d;
    int u;
    bool operator< (const HeapNode & rhs )const {
    return d>rhs.d;}
};
void dij(int src,int n,int *low)
{
    memset(visit,0,sizeof(visit));
    priority_queue <HeapNode> q;
    for(int i=0;i<=n;i++)
        low[i]=INF;
        low[src]=0;
      q.push((HeapNode){0,src});
      //cout<<"src== "<<src<<endl;
      int k=0;
    while(k<n&&!q.empty())
    {
        HeapNode x=q.top();
        q.pop();
        int u=x.u;
        //cout<<"u== "<<u<<endl;
        if(visit[u]==1)
            continue;
        visit[u]=1;
        k++;
        for(int i=0;i<G[u].size();i++)
        {
            Edge &e= edges[G[u][i]];
            //cout<<"e.from== "<<e.from<<endl;
            //cout<<"e.to==   "<<e.to<<endl;
            //cout<<"low[u]==    "<<low[u]<<endl;
           // cout<<"low[e.to]== "<<low[e.to]<<endl;
           // cout<<"e.dist==    "<<e.dist<<endl;
            if(visit[e.to]==0&&low[e.to]>low[u]+e.dist)
            {
                low[e.to]=low[u]+e.dist;
                q.push((HeapNode){low[e.to],e.to});
            }
        }
    }
}
int main()
{
    int cnt=1;
    while(scanf("%d",&n)!=EOF)
    {
        init();
        scanf("%d",&m);
        if(n==0&&m==0)
            break;
        int i,j;
        int c,a,b;
        scanf("%d%d%d",&c,&a,&b);
        int u,v,len;
        for(i=1; i<=m; i++)
        {
            scanf("%d%d%d",&u,&v,&len);
            AddEdge(u,v,len);
            AddEdge(v,u,len);
        }
        //cout<<"this is one"<<endl;
        dij(c,n,dislenc);
        dij(a,n,dislena);
        dij(b,n,dislenb);
        //cout<<"this is two"<<endl;
        printf("Scenario #%d\n",cnt++);
        if(dislenc[b]==INF||dislenc[a]==INF)
        {
            printf("Can not reah!\n");
            continue;
        }
        long long  res=INF1*3;
        for(i=1; i<=n; i++)
        {
            long long k=dislena[i]+dislenb[i]+dislenc[i];
            if(k<res)
                res=k;
        }
        printf("%d\n\n",res);
    }
    return 0;
}




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

hrbust 1339 Touring 最短路Dijkstra 邻接表

标签:算法   c++   dijkstra   优先队列   hrbust   

原文地址:http://blog.csdn.net/sholck222/article/details/47205583

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!