uva 10986 Sending email

最短路裸题啊,顶点数较多开不了邻接矩阵的,而且边数相对较少,稀疏图,用邻接表,写了个spfa和一个优先队列的dij,当做练手

spfa

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define N 20010
#define M 50010*2
#define INF 0x3f3f3f3f
struct edge
{ int u,v,w,next; }e[M];
int first[N],d[N],vis[N];
int n,m,s,t;

void spfa()
{
    queue <int> q;
    memset(d,0x3f,sizeof(d));
    memset(vis,0,sizeof(vis));
    d[s]=0; q.push(s); vis[s]=1;
    while(!q.empty())
    {
        int u,v,w;
        u=q.front(); q.pop();  vis[u]=0;
        for(int i=first[u]; i!=-1 ; i=e[i].next)
        {
            v=e[i].v;  w=e[i].w;
            if(d[u]+w<d[v])
            {
                d[v]=d[u]+w;
                if(!vis[v])
                { q.push(v); vis[v]=1; }
            }
        }
    }
    return ;
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int c=1; c<=T; c++)
    {
        scanf("%d%d%d%d",&n,&m,&s,&t);
        memset(first,-1,sizeof(first));
        m*=2;
        for(int i=0; i<m; i+=2)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            e[i].u=u; e[i].v=v;  e[i].w=w;
            e[i].next=first[u];
            first[u]=i;
            e[i+1].u=v; e[i+1].v=u; e[i+1].w=w;
            e[i+1].next=first[v];
            first[v]=i+1;
        }

        spfa();
        printf("Case #%d: ",c);
        if(d[t]!=INF)  printf("%d\n",d[t]);
        else           printf("unreachable\n");
    }
    return 0;
}

 

dij优先队列

#include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;
#define N 20010
#define M 50010*2
#define INF 0x3f3f3f3f
struct edge
{ int u,v,w,next; }e[M];
int first[N],d[N],done[N];
int n,m,s,t;

void dij()
{
    //最好用个 typedef pair<int,int> pii;
    priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
    memset(d,0x3f,sizeof(d));
    memset(done,0,sizeof(done));
    d[s]=0;
    q.push( make_pair(d[s],s) );
    while(!q.empty())
    {
        pair<int,int> x;  int u;
        x=q.top(); q.pop(); u=x.second;
        if(done[u]) continue;
        done[u]=1;
        for(int i=first[u]; i!=-1; i=e[i].next)
        {
            int v,w;
            v=e[i].v;  w=e[i].w;
            if(d[u]+w<d[v])
            {
                d[v]=d[u]+w;
                q.push(make_pair(d[v],v));
            }
        }
    }
    return ;
}

int main()
{
    int T;
    scanf("%d",&T);
    for(int c=1; c<=T; c++)
    {
        scanf("%d%d%d%d",&n,&m,&s,&t);
        memset(first,-1,sizeof(first));
        m*=2;
        for(int i=0; i<m; i+=2)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            e[i].u=u; e[i].v=v;  e[i].w=w;
            e[i].next=first[u];
            first[u]=i;
            e[i+1].u=v; e[i+1].v=u; e[i+1].w=w;
            e[i+1].next=first[v];
            first[v]=i+1;
        }

        dij();
        printf("Case #%d: ",c);
        if(d[t]!=INF)  printf("%d\n",d[t]);
        else           printf("unreachable\n");
    }
    return 0;
}

 

posted @ 2012-12-10 20:17  Titanium  阅读(578)  评论(0编辑  收藏  举报