zoukankan      html  css  js  c++  java
  • 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]);
        }
    }



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

  • 相关阅读:
    easyExcel入门
    UML-从需求到设计--迭代进化
    UML-操作契约总结
    102. Binary Tree Level Order Traversal
    98. Validate Binary Search Tree
    95. Unique Binary Search Trees II
    96. Unique Binary Search Trees
    94. Binary Tree Inorder Traversal
    84. Largest Rectangle in Histogram
    92. Reverse Linked List II
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3017553.html
Copyright © 2011-2022 走看看