HDU最短路总结

最近过的最短路题目稍微总结一下,顺便写一下模板,最短路算法众多有floyd、dij、bell-man、spfa,速度最快就是dij+优先队列或者dij+堆排序,spfa理论上很快o(ke)但实际并不一定不过spfa传说中有一个很NB用处就是处理带负权回路。

邻接表VS邻接矩阵:根据写题经验,如果可以用矩阵那一定是首选,矩阵速度比表快而且题目出现多重边时矩阵很好解决。
        今天不小心又遇见正向表与最短路图(HDU2433),感觉很兴奋哈,通宵刷题的感觉很好玩!遗憾的是hdu1385还没过,那是一题最短路路径问题等写过再去更新吧,最短路写得已经有点久料急着总结一下,今天就是通宵都要总结出,因为已经一天拖一天。最短路写完之后马上需要马上开始看数轮与数学料,光刷水题提高很慢。
最短路:
http://acm.hdu.edu.cn/showproblem.php?pid=1596(中等)
http://acm.hdu.edu.cn/showproblem.php?pid=2433(稍难)
http://acm.hdu.edu.cn/showproblem.php?pid=2112(基础)
http://acm.hdu.edu.cn/showproblem.php?pid=1874(基础)
http://acm.hdu.edu.cn/showproblem.php?pid=2544(基础)
http://acm.hdu.edu.cn/showproblem.php?pid=2680(中等)
http://acm.hdu.edu.cn/showproblem.php?pid=2647(稍难)
http://acm.hdu.edu.cn/showproblem.php?pid=1690(中等)
拓扑排序:
http://acm.hdu.edu.cn/showproblem.php?pid=3342(基础)
http://acm.hdu.edu.cn/showproblem.php?pid=1285(稍难)
最短路模版(复杂度O(nlogn))dij+优先队列:
HDU1690
#include
#include
#include
#define inf 10000000000000
using namespace std;
__int64 mp[101][101];
__int64 d[101];
bool hash[101];
__int64 a[101];
__int64 l1,l2,l3,l4,c1,c2,c3,c4;
__int64 n,m;
__int64 start,end;
typedef struct node
{            
    __int64 adj;
    __int64 w;
    struct node*next;
}node,*pnode;
typedef struct Heap
{
    bool operator<(Heap T)const
    {
        return T.dis
    }
    __int64 x;
    __int64 dis;
}Heap;
priority_queueQ;
__int64 bfs()
{
    __int64 i,j;
    for(i=0;i<=100;i++)
        hash[i]=0,d[i]=inf;
    Heap min,in;
    pnode p;
    while(!Q.empty())
        Q.pop();
    in.x=start;
    in.dis=0;
    d[start]=0;
    Q.push(in);
    while(!Q.empty())
    {
        min=Q.top();
        Q.pop();
        if(min.x==end)
            return min.dis;
        if(hash[min.x])
            continue;
        hash[min.x]=true;
        for(int i=1;i<=n;i++)
        {
            if(d[i]>mp[min.x][i]+d[min.x]&&!hash[i])
            {
                in.x=i;
                in.dis=mp[min.x][i]+d[min.x];
                d[i]=in.dis;
                Q.push(in);
            }
        }
        
    }
    return -1;
}
int judge(int dis)
{
    if(dis>0&&dis<=l1)
        return c1;
    else
        if(dis>l1&&dis<=l2)
            return c2;
        else
            if(dis>l2&&dis<=l3)
                return c3;
            else if(dis>l3&&dis<=l4)
                return c4;
            else
                return -1;
}
int main()
{
    __int64 p=1,i,j;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&l1,&l2,&l3,&l4,&c1,&c2,&c3,&c4);
        scanf("%I64d%I64d",&n,&m);
        for(i=0;i<=n;i++)
            for(j=0;j<=n;j++)
                mp[i][j]=inf;
        for(i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                __int64 l=a[i]-a[j];
                if(l<0)
                    l=-l;
                __int64 s=judge(l);
                if(s<0) s=inf;
                mp[i][j]=s;
                mp[j][i]=s;
            }
        }
        printf("Case %I64d:\n",p++);
        for(i=0;i
        {
            scanf("%I64d%I64d",&start,&end);
            bfs();
            if(d[end]==inf)
                printf("Station %I64d and station %I64d are not attainable.\n",start,end);
            else
                printf("The minimum cost between station %I64d and station %I64d is %I64d.\n",start,end,d[end]);
        }
    }
    return 0;
}
SPFA模版:
HDU1874
#define MAX 999999999
#include
#include
#include
using namespace std;
int n,m,mp[1001][1001],d[1001];
bool visit[201];
void init()
{
    int i,j;
    for(i=0;i<=n;i++)
        for(j=0;j<=n;j++)
            mp[i][j]=MAX;
        for(i=0;i<=n;i++)
            d[i]=MAX,visit[i]=0;
}
void spfa(int s)
{
    queue k;
    int i,j;
    k.push(s);
    d[s]=0;
    visit[s]=1;
    while(!k.empty())
    {
        int h=k.front();
        k.pop();
        visit[h]=0;
        for(i=0;i
        {
            if(d[i]-mp[h][i]>d[h])
            {
                d[i]=mp[h][i]+d[h];
                if(!visit[i])
                {
                    k.push(i);
                    visit[i]=1;
                }
            }
        }
    }
}
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        int x,y,w;
        for(i=0;i
        {
            scanf("%d%d%d",&x,&y,&w);
            mp[x][y]=mp[y][x]=(w
        }
        int s,t;
        scanf("%d%d",&s,&t);
        spfa(s);
        if(d[t]==MAX) puts("-1");
        else printf("%d\n",d[t]);
    }
    return 0;
}
HDU3342(拓扑排序模版)
#include
#include
#include
using namespace std;
struct khp
{
    int in;
    set k;
};
khp y[101];
bool final[101];
int n,m;
bool topu()
{
    int t,s=0,i,j;
    bool judge=false;
    for(i=0;i
        if(y[i].in==0)
        {
            t=i;
            break;
        }
        
        if(i==n) return false;
        final[t]=true;
        while(s
        {
            judge=false;
        for(set::iterator it=y[t].k.begin();it!=y[t].k.end();it++)
            y[*it].in--;
        for(i=0;i
            if(y[i].in==0&&!final[i])
            {
                t=i;
                s++;
                final[t]=true;
                judge=true;
                break;
            }
        if(!judge)
            break;
        }
        if(s!=n-1)
            return false;
        else return true;
}
int main()
{
    int i,j,a,b;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0;i<101;i++)
        {
            final[i]=false;
            y[i].in=0;
            y[i].k.clear();
        }
        if(!n&&!m) break;
        for(i=0;i
        {
            scanf("%d%d",&a,&b);
            if(!y[b].k.count(a))
            {
                y[b].k.insert(a);
                y[a].in++;
            }
        }
        bool judge=topu();
        if(!judge)
            puts("NO");
        else puts("YES");
    }
    return 0
}

你可能感兴趣的:(最短路问题)