PKU1511(Invitation Cards)最短路徑-鄰接表+SPFA算法

/******************************************************
題目大意:
弱菜英語又是渣渣,題意是對着測試數據連猜帶蒙出來的;
在一個有向圖中,要求頂點1到其他的所有頂點往返的總共的最小花費;
另外給出的數據量略大,頂點數和邊數都是1000000的範圍;

算法分析:
這是一個最短路徑問題;
由於數據範圍很大,用鄰接矩陣和dijkstra很可能超時超內存,所以採用鄰接表建圖;
在這個單向圖中,求的是從第一個頂點到其他所有頂點的最短路徑和其他所有頂點到第一個頂點的最短路徑;
所以用兩個鄰接表分別寸正向圖和反向圖;
然後兩次運用SPFA算法求以頂點1爲起點的最短路徑;
他們的和就是所求的最小花費;
*******************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
using namespace std;

const int N=1000005;
const int INF=1000000000;

typedef long long LL;

struct edge
{
    int to;
    int w;
    edge *next;
};

edge *G1[N],*G2[N];//正向圖,反向圖

int inq[N];//每個頂點是否在隊列中的標誌
int n,m;
LL dist[N];
LL ans;

queue<int>Q;

void SPFA(int flag)
{
    int v=1;//v爲起點
    for(int i=1; i<=n; i++)
    {
        dist[i]=INF;
        inq[i]=0;
    }
    dist[v]=0;
    inq[v]=1;
    Q.push(v);
    edge *temp;
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        inq[u]=0;
        if(flag==1)
            temp=G1[u];
        else
            temp=G2[u];
        while(temp!=NULL)
        {
            int x=temp->to;
            if(dist[x]>dist[u]+temp->w)//鬆弛
            {
                dist[x]=dist[u]+temp->w;
                if(!inq[x])
                {
                    Q.push(x);
                    inq[x]=1;
                }
            }
            temp=temp->next;
        }
    }
    for(int i=1; i<=n; i++)
    {
       // printf("dist[%d]==%d\n",i,dist[i]);
        ans+=dist[i];
    }
}

int main()
{
    //freopen("C:\\Users\\Administrator\\Desktop\\kd.txt","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(G1,0,sizeof(G1));
        memset(G2,0,sizeof(G2));
        scanf("%d%d",&n,&m);
        int u,v,w;
        edge *temp1,*temp2;
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            temp1=new edge;//正向圖
            temp1->to=v;
            temp1->w=w;
            temp1->next=NULL;
            if(G1[u]==NULL)
                G1[u]=temp1;
            else
            {
                temp1->next=G1[u];
                G1[u]=temp1;
            }

            temp2=new edge;//反向圖
            temp2->to=u;
            temp2->w=w;
            temp2->next=NULL;
            if(G2[v]==NULL)
                G2[v]=temp2;
            else
            {
                temp2->next=G2[v];
                G2[v]=temp2;
            }
        }
        ans=0;
        SPFA(1);//正向
        SPFA(0);//反向
        printf("%lld\n",ans);
        /*for(int i=1; i<=n; i++) //釋放空間竟然釋放的超時了。。
        {
            temp1=G1[i];
            while(temp1!=NULL)
            {
                G1[i]=temp1->next;
                delete temp1;
                temp1=G1[i];
            }
            temp2=G2[i];
            while(temp2!=NULL)
            {
                G2[i]=temp2->next;
                delete temp2;
                temp2=G2[i];
            }
        }*/
    }
    return 0;



/**************************************************************
    Problem: 1511
    User: Jarily
    Language: C++
    Result: Accepted
    Time: 1938 MS
    Memory: 43796K
****************************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
using namespace std;

const int N=1000005;
const int INF=1000000000;

typedef long long LL;

struct edge
{
    int to;
    int w;
    int next;
};

edge G[2][N];//正向圖,反向圖
int head[2][N];

int inq[N];//每個頂點是否在隊列中的標誌
int n,m;
LL dist[N];
LL ans;

void Addedge(int u,int v,int w,int idx)
{
    G[0][idx].w=w;
    G[0][idx].to=v;
    G[0][idx].next=head[0][u];
    head[0][u]=idx;

    G[1][idx].w=w;
    G[1][idx].to=u;
    G[1][idx].next=head[1][v];
    head[1][v]=idx;
}

queue<int>Q;

void SPFA(int flag)
{
    int v=1;//v爲起點
    for(int i=1; i<=n; i++)
    {
        dist[i]=INF;
        inq[i]=0;
    }
    dist[v]=0;
    inq[v]=1;
    Q.push(v);
    int temp;
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        inq[u]=0;
        temp=head[flag][u];
        int x,y;
        while(temp!=-1)
        {
            x=G[flag][temp].to;
            if(dist[x]>dist[u]+G[flag][temp].w)//鬆弛
            {
                dist[x]=dist[u]+G[flag][temp].w;
                if(!inq[x])
                {
                    Q.push(x);
                    inq[x]=1;
                }
            }
            temp=G[flag][temp].next;
        }
    }
    for(int i=1; i<=n; i++)
    {
        // printf("dist[%d]==%d\n",i,dist[i]);
        ans+=dist[i];
    }
}

int main()
{
    // freopen("C:\\Users\\Administrator\\Desktop\\kd.txt","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(G,0,sizeof(G));
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        int u,v,w;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            Addedge(u,v,w,i);
        }
        ans=0;
        SPFA(0);//正向
        SPFA(1);//反向
        printf("%lld\n",ans);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章