• hdu_4396,二维最短路


    http://acm.hdu.edu.cn/showproblem.php?pid=4396

    比赛的时候怎么写怎么错。。还TLE。。还MLE。。还RE。。

    dijkstra
    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    
    const int maxn(5005);
    const int maxe(100010);
    const int inf(100000000);
    struct Edge{
        int v, w, next;
    }e[maxe*2];
    int head[maxn], cnt;
    int n, m;
    int s, t, k;
    
    void init(){
        memset(head, -1, sizeof head);
        cnt = 0;
    }
    void add_Edge(int u, int v, int w){
        e[cnt].v = v, e[cnt].w = w, e[cnt].next = head[u];
        head[u] = cnt ++;
    }
    struct Point{
        int u, w, c;
        friend bool operator < (const Point &a, const Point &b){
            return a.w > b.w;
        }
    }dist[maxn][55];
    int vis[maxn][55];
    void dijk(){
        memset(vis, 0, sizeof vis);
        for(int i = 0; i <= n; i ++){
            for(int j = 0; j <= k; j ++){
                dist[i][j].w = inf;
                dist[i][j].u = i;
                dist[i][j].c = j;
            }
        }
        priority_queue < Point > q;
        q.push(dist[s][0]);
        dist[s][0].w = 0;
        while(!q.empty()){
            Point tmp = q.top(); q.pop();
            if(vis[tmp.u][tmp.c])continue;
            vis[tmp.u][tmp.c] = 1;
            if(tmp.u == t && tmp.c == k) break;
            for(int i = head[tmp.u]; i + 1; i = e[i].next){
                int v = e[i].v, w = e[i].w;
                if(tmp.c < k){
                    if(vis[v][tmp.c+1]) continue;
                    if(dist[v][tmp.c+1].w > w + dist[tmp.u][tmp.c].w){
                        dist[v][tmp.c+1].w = w + dist[tmp.u][tmp.c].w;
                        q.push(dist[v][tmp.c+1]);
                    }
                }else
                  if(vis[v][k]) continue;
                  else
                    if(dist[v][k].w > w + dist[tmp.u][k].w){
                        dist[v][k].w = w + dist[tmp.u][k].w;
                        q.push(dist[v][k]);
                    }
            }
        }
        if(dist[t][k].w >= inf) puts("-1");
        else
          printf("%d\n", dist[t][k].w);
    }
    void input(){
        init();
        for(int i = 0; i < m; i ++){
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            add_Edge(u, v, w);
            add_Edge(v, u, w);
        }
        scanf("%d%d%d", &s, &t, &k);
        k = k/10 + (k%10 != 0);
    }
    int main(){
        while(~scanf("%d%d", &n, &m)){
            input();
            dijk();
        }
        return 0;
    }
    spfa
    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    
    const int maxn(5005);
    const int maxe(100010);
    const int inf(100000000);
    struct Edge{
        int v, w, next;
    }e[maxe*2];
    int head[maxn], cnt;
    int n, m;
    int s, t, k;
    
    void init(){
        memset(head, -1, sizeof head);
        cnt = 0;
    }
    void add_Edge(int u, int v, int w){
        e[cnt].v = v, e[cnt].w = w, e[cnt].next = head[u];
        head[u] = cnt ++;
    }
    struct Point{
        int u, w, c;
        Point(int x, int y, int z) : u(x), w(y), c(z){}
        //friend bool operator < (const Point &a, const Point &b){
        //    return a.w > b.w;
        //}
    };
    int dist[maxn][55];
    int vis[maxn][55];
    void spfa(){
        queue < Point > q;
        memset(vis, 0, sizeof vis);
        for(int i = 0; i <= n; i ++)
          for(int j = 0; j <= 60; j ++)
            dist[i][j] = inf;
        vis[s][0] = 1;
        dist[s][0] = 0;
        q.push(Point(s, 0, 0));
        while(!q.empty()){
            Point tmp = q.front(); q.pop();
            int u = tmp.u;
            int w = tmp.w;
            int c = tmp.c;
            vis[u][c] = 0;
            int cc = c;
            for(int i = head[u]; i + 1; i = e[i].next){
                int v = e[i].v;
                int cost = dist[u][cc] + e[i].w;
                if(cc + 1 > k )
                  c = k;
                else
                  c = cc + 1;
                if(dist[v][c] > cost){
                    dist[v][c] = cost;
                    if(!vis[v][c]){
                        vis[v][c] = 1;
                        q.push(Point(v, dist[v][c], c));
                    }
                }
            }
        }
        if(dist[t][k] == inf) puts("-1");
        else
          printf("%d\n", dist[t][k]);
    }
    void input(){
        init();
        for(int i = 0; i < m; i ++){
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            add_Edge(u, v, w);
            add_Edge(v, u, w);
        }
        scanf("%d%d%d", &s, &t, &k);
        k = k/10 + (k%10 != 0);
    }
    int main(){
        while(~scanf("%d%d", &n, &m)){
            input();
            spfa();
        }
        return 0;
    }
  • 相关阅读:
    Flutter动画(1)动画基础介绍(重要)
    flutter AnimationController动画1
    flutter 动画示例
    Nginx 搭建图片服务器
    dart中list的map方法获取index
    Flutter 视频缩略图
    Flutter:设置字体不随系统字体大小进行改变
    Flutter 屏幕适配flutter_screenutil使用心得
    flutter 软键盘弹起导致定位底部按钮浮动在键盘上的问题
    C++ 代码小技巧(一)
  • 原文地址:https://www.cnblogs.com/louzhang/p/2654058.html
Copyright © 2020-2023  润新知