SPFA几道经典题目

HDU 4360

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define eps 1e-5
#define MAXN 2222
#define MAXM 95555
#define INF 200000000000000LL
using namespace std;
struct EDGE
{
    int v, next;
    long long w;
    int id;
} edge[MAXM];
struct P
{
    int id, u;
    P(){}
    P(int a, int b) {u = a; id = b;}
};
int head[MAXN], e, n, m;
void init()
{
    e = 0;
    memset(head, -1, sizeof(head));
}
void add(int x, int y, long long w, char c)
{
    edge[e].v = y;
    edge[e].w = w;
    edge[e].next = head[x];
    if(c == 'L') edge[e].id = 0;
    if(c == 'O') edge[e].id = 1;
    if(c == 'V') edge[e].id = 2;
    if(c == 'E') edge[e].id = 3;
    head[x] = e++;
}
long long d[MAXN][4];
int vis[MAXN][4], num[MAXN][4];
P q[MAXN * 100];
void spfa(int src)
{
    int h = 0, t = 0;
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 4; j++)
            d[i][j] = INF, vis[i][j] = 0, num[i][j] = 0;
    vis[src][3] = 1;
    d[src][3] = 0;
    P tmp = P(src, 3);
    q[t++] = tmp;
    while(h < t)
    {
        tmp = q[h++];
        int u = tmp.u;
        int id = tmp.id;
        vis[u][id] = 0;
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].v;
            int x = edge[i].id;
            long long w = edge[i].w;
            if((d[u][id] + w < d[v][x] || d[v][x] == 0) && (id + 1) % 4 == x)
            {
                d[v][x] = d[u][id] + w;
                num[v][x] = num[u][id];
                if(x == 3) num[v][x]++;
                if(!vis[v][x])
                {
                    q[t++] = P(v, x);
                    vis[v][x] = 1;
                }
            }
            else if((d[u][id] + w == d[v][x] || d[v][x] == 0) && (id + 1) % 4 == x && num[v][x] <= num[u][id])
            {
                num[v][x] = num[u][id];
                if(x == 3) num[v][x]++;
                if(!vis[v][x])
                {
                    q[t++] = P(v, x);
                    vis[v][x] = 1;
                }
            }
        }
    }
}
int main()
{
    int T, cas = 0;
    scanf("%d", &T);
    while(T--)
    {
        init();
        scanf("%d%d", &n, &m);
        int u, v, w;
        char s[5];
        while(m--)
        {
            scanf("%d%d%d%s", &u, &v, &w, s);
            add(u, v, w, s[0]);
            add(v, u, w, s[0]);
        }
        spfa(1);
        if(num[n][3] == 0 || d[n][3] == INF)
            printf("Case %d: Binbin you disappoint Sangsang again, damn it!\n", ++cas);
        else
            printf("Case %d: Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding %d LOVE strings at last.\n", ++cas, d[n][3], num[n][3]);
    }
    return 0;
}


HDU 4396

#include<cstdio>
#include<queue>
#include<cstring>
#define M 100100
#define N 5010
#define inf 100000000
using namespace std;
typedef long long ll;
int n,m,cnt;
int s,t,ts;
int ans;
struct Edge{
    int v,c,next;
}edge[M*2];
int head[N];
int dp[N][52];
bool vis[N][52];
struct Point{
    int v,bian;
};
void addedge(int u,int v,int len){
    edge[cnt].v=v;
    edge[cnt].c=len;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].v=u;
    edge[cnt].c=len;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
void init(){
    memset(head,-1,sizeof(head));
    cnt=0;
    ans=inf;
    for(int i=1;i<=n;i++)
        for(int j=0;j<=51;j++)
            dp[i][j]=inf;
}

void bfs(){
    int i,j;
    queue<struct Point>q;
    struct Point tem,tt;
    
    tem.v=s;
    tem.bian=0;
    q.push(tem);
    dp[s][0]=0;
    for(i=1;i<=n;i++)
        for(j=0;j<=51;j++)
            vis[i][j]=0;
    vis[s][0]=1;
    while(!q.empty()){
        tem=q.front();
        q.pop();
        vis[tem.v][tem.bian]=0;
        int u=tem.v;
        for(i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].v;
            tt.v=v;
            if(tem.bian==ts)
                tt.bian=tem.bian;
            else
                tt.bian=tem.bian+1;
            if(dp[v][tt.bian]>dp[u][tem.bian]+edge[i].c){
                dp[v][tt.bian]=dp[u][tem.bian]+edge[i].c;
                if(!vis[v][tt.bian]){
                    vis[v][tt.bian]=1;
                    q.push(tt);
                }
            }
        }
    }
}
int main(){
    int u,v,w;
    int i;
    while(scanf("%d %d",&n,&m)==2){
        init();
        for(i=1;i<=m;i++){
            scanf("%d %d %d",&u,&v,&w);
            addedge(u,v,w);
        }
        scanf("%d %d %d",&s,&t,&ts);
        if(ts%10==0)
            ts=ts/10;
        else
            ts=ts/10+1;
        bfs();
        if(dp[t][ts]==inf)
            printf("-1\n");
        else
            printf("%d\n",dp[t][ts]);
    }
}



这篇文章我之前应该是在博客上发过的,但是今天用的时候怎么找了找不到,重新整理了一下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值