codevs 1557 热浪(最短路【模板】)

题目描述 Description
德克萨斯纯朴的民眾们这个夏天正在遭受巨大的热浪!!!他们的德克萨斯长角牛吃起来不错,可是他们并不是很擅长生產富含奶油的乳製品。Farmer John此时以先天下之忧而忧,后天下之乐而乐的精神,身先士卒地承担起向德克萨斯运送大量的营养冰凉的牛奶的重任,以减轻德克萨斯人忍受酷暑的痛苦。
FJ已经研究过可以把牛奶从威斯康星运送到德克萨斯州的路线。这些路线包括起始点和终点先一共经过T (1 <= T <= 2,500)个城镇,方便地标号為1到T。除了起点和终点外地每个城镇由两条双向道路连向至少两个其它地城镇。每条道路有一个通过费用(包括油费,过路费等等)。
给定一个地图,包含C (1 <= C <= 6,200)条直接连接2个城镇的道路。每条道路由道路的起点Rs,终点Re (1 <= Rs <= T; 1 <= Re <= T),和花费(1 <= Ci <= 1,000)组成。求从起始的城镇Ts (1 <= Ts <= T)到终点的城镇Te(1 <= Te <= T)最小的总费用。

输入描述 Input Description
第一行: 4个由空格隔开的整数: T, C, Ts, Te
第2到第C+1行: 第i+1行描述第i条道路。有3个由空格隔开的整数: Rs, Re和Ci

输出描述 Output Description
一个单独的整数表示从Ts到Te的最小总费用。数据保证至少存在一条道路。

样例输入 Sample Input
7 11 5 4
2 4 2
1 4 3
7 2 2
3 4 3
5 7 5
7 3 3
6 1 1
6 3 4
2 4 3
5 6 3
7 2 1

样例输出 Sample Output
7

数据范围及提示 Data Size & Hint
5->6->1->4 (3 + 1 + 3)

题解:最短路,spfa、dijkstra都能做(但是不得不说数据好水啊)
不过我只是想写几个模板233
spfa(slf)和dijkstra(堆)

代码如下
1.spfa

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int MAXV=2500+500;
const int MAXE=6200+500;
int first[MAXV],nxt[MAXE<<1],d[MAXV];
bool used[MAXV];
int T,C,Ts,Te,tot;
struct edge
{
    int from,to,cost;
}es[MAXE<<1];
void init()
{
    memset(first,-1,sizeof(first));
    memset(used,0,sizeof(used));
    memset(d,63,sizeof(d));
    tot=0;
}
void build(int f,int t,int d)
{
    es[++tot]=(edge){f,t,d};
    nxt[tot]=first[f];
    first[f]=tot;
}
queue<int> Q;
void spfa(int s)
{
    d[s]=0;
    Q.push(s);
    used[s]=1;
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        used[u]=0;
        for(int i=first[u];i!=-1;i=nxt[i])
        {
            int v=es[i].to;
            if(d[v]>d[u]+es[i].cost)
            {
                d[v]=d[u]+es[i].cost;
                if(!used[v])
                {
                    Q.push(v);
                    used[v]=1;
                }
            }
        }
    }
}
int main()
{
    scanf("%d%d%d%d",&T,&C,&Ts,&Te);
    init();
    for(int i=1;i<=C;i++)
    {
        int Rs,Re,Ci;
        scanf("%d%d%d",&Rs,&Re,&Ci);
        build(Rs,Re,Ci);
        build(Re,Rs,Ci);
    }
    spfa(Ts);
    printf("%d",d[Te]);
    return 0;
}

2.spfa+slf

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int MAXV=2500+500;
const int MAXE=6200+500;
int first[MAXV],nxt[MAXE<<1],d[MAXV];
bool used[MAXV];
int T,C,Ts,Te,tot;
struct edge
{
    int from,to,cost;
}es[MAXE<<1];
void init()
{
    memset(first,-1,sizeof(first));
    memset(d,63,sizeof(d));
    tot=0;
}
void build(int f,int t,int d)
{
    es[++tot]=(edge){f,t,d};
    nxt[tot]=first[f];
    first[f]=tot;
}
deque<int> Q;
void spfa(int s)
{
    d[s]=0;
    Q.push_front(s);
    used[s]=1;
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop_front();
        used[u]=0;
        for(int i=first[u];i!=-1;i=nxt[i])
        {
            int v=es[i].to;
            if(d[v]>d[u]+es[i].cost)
            {
                d[v]=d[u]+es[i].cost;
                if(!used[v])
                {
                    used[v]=1;
                    if(Q.empty()) Q.push_back(v);
                    else if(d[Q.front()]<d[v]) Q.push_back(v);
                    else Q.push_front(v);
                }
            }
        }
    }
}
int main()
{
    scanf("%d%d%d%d",&T,&C,&Ts,&Te);
    init();
    for(int i=1;i<=C;i++)
    {
        int Rs,Re,Ci;
        scanf("%d%d%d",&Rs,&Re,&Ci);
        build(Rs,Re,Ci);
        build(Re,Rs,Ci);
    }
    spfa(Ts);
    printf("%d",d[Te]);
    return 0;
}

3.dijkstra

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int MAXV=2500+500;
const int MAXE=6200+500;
int first[MAXV],nxt[MAXE<<1],d[MAXV];
bool used[MAXV];
int T,C,Ts,Te,tot;
struct edge
{
    int from,to,cost;
}es[MAXE<<1];
void init()
{
    memset(first,-1,sizeof(first));
    memset(used,0,sizeof(used));
    memset(d,63,sizeof(d));
    tot=0;
}
void build(int f,int t,int d)
{
    es[++tot]=(edge){f,t,d};
    nxt[tot]=first[f];
    first[f]=tot;
}
void dijkstra(int s)
{
    d[s]=0;
    while(true)
    {
        int v=-1;
        for(int i=1;i<=T;i++)
            if(!used[i]&&(d[v]>d[i]||v==-1)) v=i;
        if(v==-1) return ;
        used[v]=1;
        for(int i=first[v];i!=-1;i=nxt[i])
        {
            int u=es[i].to;
            if(d[u]>d[v]+es[i].cost)
                d[u]=d[v]+es[i].cost;
        }
    }
}
int main()
{
    scanf("%d%d%d%d",&T,&C,&Ts,&Te);
    init();
    for(int i=1;i<=C;i++)
    {
        int Rs,Re,Ci;
        scanf("%d%d%d",&Rs,&Re,&Ci);
        build(Rs,Re,Ci);
        build(Re,Rs,Ci);
    }
    dijkstra(Ts);
    printf("%d",d[Te]);
    return 0;
}

4.dijkstra+堆

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int MAXV=2500+500;
const int MAXE=6200+500;
int first[MAXV],nxt[MAXE<<1],d[MAXV];
bool used[MAXV];
int T,C,Ts,Te,tot;
struct edge
{
    int from,to,cost;
}es[MAXE<<1];
struct heap
{
    int num,dis;
};
void init()
{
    memset(first,-1,sizeof(first));
    memset(used,0,sizeof(used));
    memset(d,63,sizeof(d));
    tot=0;
}
void build(int f,int t,int d)
{
    es[++tot]=(edge){f,t,d};
    nxt[tot]=first[f];
    first[f]=tot;
}
bool operator < (heap a,heap b)
{
    return a.dis>b.dis;
}
priority_queue<heap> Q;
void dijkstra(int s)
{
    d[s]=0;
    Q.push((heap){s,d[s]});
    while(!Q.empty())
    {
        heap v=Q.top();
        used[v.num]=1;
        Q.pop();
        for(int i=first[v.num];i!=-1;i=nxt[i])
        {
            int u=es[i].to;
            if(d[u]>d[v.num]+es[i].cost)
            {
                d[u]=d[v.num]+es[i].cost;
                if(!used[u])
                    Q.push((heap){u,d[u]});
            }
        }
    }
}
int main()
{
    scanf("%d%d%d%d",&T,&C,&Ts,&Te);
    init();
    for(int i=1;i<=C;i++)
    {
        int Rs,Re,Ci;
        scanf("%d%d%d",&Rs,&Re,&Ci);
        build(Rs,Re,Ci);
        build(Re,Rs,Ci);
    }
    dijkstra(Ts);
    printf("%d",d[Te]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值