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

最短路 HDU2544(练习模板的好题)

时间:2015-04-11 14:55:41      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

最短路
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 37479 Accepted Submission(s): 16317


Problem Description
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?



Input
输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。


Output
对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间


Sample Input

2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2

0 0

Sample Output

3

2

SPFA算法SPFA比较简单,这篇文章写的不错

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<cstring>
using namespace std;
template<class T>inline T read(T&x)
{
    char c;
    while((c=getchar())<=32)if(c==EOF)return -1;
    bool ok=false;
    if(c=='-')ok=true,c=getchar();
    for(x=0; c>32; c=getchar())
        x=x*10+c-'0';
    if(ok)x=-x;
    return 1;
}
template<class T> inline T read_(T&x,T&y)
{
    return read(x)!=-1&&read(y)!=-1;
}
template<class T> inline T read__(T&x,T&y,T&z)
{
    return read(x)!=-1&&read(y)!=-1&&read(z)!=-1;
}
template<class T> inline void write(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
    write(x);
    putchar('\n');
}
//-------ZCC IO template------
const int maxn=101;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long  LL;
typedef double DB;
#define bug printf("---\n");
#define mod 10007

int G[maxn][maxn];
bool vis[maxn];
int d[maxn];
int V,E;
void spfa(int s)
{
    For(i,0,V+1)
    {
        vis[i]=false;
        d[i]=inf;
    }
    d[s]=0;
    queue<int> q;
    q.push(s);
    vis[s]=true;
    while(!q.empty())
    {
        int tmp=q.front();q.pop();
        for(int u=1;u<=V;u++)
        {
            if(d[u]>d[tmp]+G[tmp][u])
            {
                d[u]=d[tmp]+G[tmp][u];
                if(!vis[u])
                {
                    q.push(u);
                    vis[u]=true;
                }
            }
        }
        vis[tmp]=false;
    }

}

int main()
{
  //#ifndef ONLINE_JUDGE
   //freopen("in.txt","r",stdin);
 // #endif // ONLINE_JUDGE
    int n,m,i,j,k,t;
    while(read_(V,E)&&(V||E))
    {
        For(i,1,V+1)For(j,1,V+1)G[i][j]=inf;
        for(i=0;i<E;i++)
        {
            int a,b,c;
            read__(a,b,c);
            G[a][b]=c;
            G[b][a]=c;
        }
        spfa(1);
        writeln(d[V]);
    }
    return 0;
}
bell_man  时间O(V*E)

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
using namespace std;
template<class T>inline T read(T&x)
{
    char c;
    while((c=getchar())<=32)if(c==EOF)return -1;
    bool ok=false;
    if(c=='-')ok=true,c=getchar();
    for(x=0; c>32; c=getchar())
        x=x*10+c-'0';
    if(ok)x=-x;
    return 1;
}
template<class T> inline T read_(T&x,T&y)
{
    return read(x)!=-1&&read(y)!=-1;
}
template<class T> inline T read__(T&x,T&y,T&z)
{
    return read(x)!=-1&&read(y)!=-1&&read(z)!=-1;
}
template<class T> inline void write(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
    write(x);
    putchar('\n');
}
//-------ZCC IO template------
const int maxn=1000110;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long  LL;
typedef double DB;
#define bug printf("---\n");
#define mod 10007

struct edge
{
    int from,to,cost;
}es[maxn];

int V,E;
int d[maxn];
void bell_man(int s)
{
    fill(d,d+V+1,inf);
    d[s]=0;
    while(true)
    {
        bool update=false;
        for(int i=0;i<E;i++)
        {
            edge e=es[i];
            if(d[e.from]!=inf&&d[e.to]>d[e.from]+e.cost)
            {
                d[e.to]=d[e.from]+e.cost;
                update=true;
            }
        }
        if(!update)break;
    }
}



int main()
{
  //#ifndef ONLINE_JUDGE
   //freopen("in.txt","r",stdin);
 // #endif // ONLINE_JUDGE
    int n,m,i,j,k,t;
    while(read_(V,E)&&(V||E))
    {
        E<<=1;
        for(i=0;i<E;i+=2)
        {
            int a,b,c;
            read__(a,b,c);
            es[i].from=a;
            es[i].to=b;
            es[i].cost=c;
            es[i+1].from=b;
            es[i+1].to=a;
            es[i+1].cost=c;
        }
        bell_man(1);
        writeln(d[V]);
    }
    return 0;
}


dijkstra  时间O(V*V)

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
using namespace std;
template<class T>inline T read(T&x)
{
    char c;
    while((c=getchar())<=32)if(c==EOF)return -1;
    bool ok=false;
    if(c=='-')ok=true,c=getchar();
    for(x=0; c>32; c=getchar())
        x=x*10+c-'0';
    if(ok)x=-x;
    return 1;
}
template<class T> inline T read_(T&x,T&y)
{
    return read(x)!=-1&&read(y)!=-1;
}
template<class T> inline T read__(T&x,T&y,T&z)
{
    return read(x)!=-1&&read(y)!=-1&&read(z)!=-1;
}
template<class T> inline void write(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
    write(x);
    putchar('\n');
}
//-------ZCC IO template------
const int maxn=101;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long  LL;
typedef double DB;
#define bug printf("---\n");
#define mod 10007

int G[maxn][maxn];
bool vis[maxn];
int d[maxn];
int V,E;
void dij(int s)
{
    For(i,0,V+1)
    {
        vis[i]=false;
        d[i]=inf;
    }
    d[s]=0;
    while(true)
    {
        int v=-1;
        For(u,1,V+1) if(!vis[u]&&(v==-1||d[v]>d[u]))
            v=u;
        if(v==-1)break;
        vis[v]=true;
        For(u,1,V+1)
            d[u]=min(d[u],d[v]+G[v][u]);
    }
}

int main()
{
  //#ifndef ONLINE_JUDGE
   //freopen("in.txt","r",stdin);
 // #endif // ONLINE_JUDGE
    int n,m,i,j,k,t;
    while(read_(V,E)&&(V||E))
    {
        For(i,1,V+1)For(j,1,V+1)G[i][j]=inf;
        for(i=0;i<E;i++)
        {
            int a,b,c;
            read__(a,b,c);
            G[a][b]=c;
            G[b][a]=c;
        }
        dij(1);
        writeln(d[V]);
    }
    return 0;
}


dijkstra使用堆优化版 时间O(E log V)

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<cstdlib>
#include<set>
#include<stack>
#include<cstring>
using namespace std;
template<class T>inline T read(T&x)
{
    char c;
    while((c=getchar())<=32)if(c==EOF)return -1;
    bool ok=false;
    if(c=='-')ok=true,c=getchar();
    for(x=0; c>32; c=getchar())
        x=x*10+c-'0';
    if(ok)x=-x;
    return 1;
}
template<class T> inline T read_(T&x,T&y)
{
    return read(x)!=-1&&read(y)!=-1;
}
template<class T> inline T read__(T&x,T&y,T&z)
{
    return read(x)!=-1&&read(y)!=-1&&read(z)!=-1;
}
template<class T> inline void write(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
    write(x);
    putchar('\n');
}
//-------ZCC IO template------
const int maxn=101;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long  LL;
typedef double DB;
typedef pair<int,int> P;
#define bug printf("---\n");
#define mod 10007

int V,E;
struct edge
{
    int to,cost;
    edge(int a,int b){to=a;cost=b;}
};
vector<edge> G[maxn];
int d[maxn];

void dij_(int s)
{
     priority_queue<P,vector<P>,greater<P> >q;
     fill(d,d+V+1,inf);
     d[s]=0;
     q.push(P(0,s));

     while(!q.empty())
     {
         P v=q.top();
         q.pop();
         if(d[v.second]<v.first)continue;
         for(int i=0;i<G[v.second].size();i++)
         {
             edge tmp=G[v.second][i];
             if(d[tmp.to]>d[v.second]+tmp.cost)
             {
                 d[tmp.to]=d[v.second]+tmp.cost;
                 q.push(P(d[tmp.to],tmp.to));
             }
         }
     }
}

int main()
{
  //#ifndef ONLINE_JUDGE
   //freopen("in.txt","r",stdin);
 // #endif // ONLINE_JUDGE
    int n,m,i,j,k,t;
    while(read_(V,E)&&(V||E))
    {
        For(i,0,E)
        {
            int a,b,c;
            read__(a,b,c);
            G[a].push_back(edge(b,c));
            G[b].push_back(edge(a,c));
        }
        dij_(1);
        writeln(d[V]);
        For(i,0,V+1)G[i].clear();
    }
    return 0;
}




最短路 HDU2544(练习模板的好题)

标签:

原文地址:http://blog.csdn.net/u013167299/article/details/44994129

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