• 数据结构实验之图论七:驴友计划【迪杰斯特拉算法】(SDUT 3363)


    分析:可以求简单的任意两点间最短距离的稍微变形,一个板子题。 

    #include <iostream>
    #include <bits/stdc++.h>
    using namespace std;
    int inf = 0x3fffff;
    int gra[1005][1005];
    int mon[1005][1005];
    int vis[1005];
    int dist[1005];
    int cost[1005];
    void dijkstra(int s,int d,int n)
    {
        memset(vis,0,sizeof(vis));
        for(int i = 0; i <= n; i ++) dist[i] = gra[s][i],cost[i] = mon[s][i];
        int Min = inf, v, Micost = inf;
        for(int i = 1; i <= n; i ++)
        {
            Min = Micost = inf;
            for(int j = 1; j <= n; j ++)
            {
                if(!vis[j]){
                    if(dist[j] < Min){Min = dist[j];
                    v = j;
                    Micost = cost[j];
                    }
                    else if(dist[j] == Min)
                    {
                        if(cost[j] < Micost) {
                            Min = dist[j];
                            v = j;
                            Micost = cost[j];
                        }
                    }
                }
            }
            vis[v] = 1;
            for(int j = 1; j <= n; j ++)
            {
                if(!vis[j]){
                    if(dist[j] > gra[v][j] + dist[v])dist[j] = gra[v][j] + dist[v],cost[j] = mon[v][j] + cost[v];
                    else if(dist[j] == gra[v][j] + dist[v]) {
                        if(cost[j] >  cost[v] + mon[v][j]){
                            dist[j] = gra[v][j] + dist[v],cost[j] = mon[v][j] + cost[v];
                        }
                    }
                }
            }
        }
        printf("%d %d
    ",dist[d], cost[d]);
    }
    int main()
    {
        int n,m,u,v,w,p,s,d,t;
        scanf("%d",&t);
        while(t--){
        scanf("%d%d%d%d",&n,&m,&s,&d);
            for(int i = 0; i<= n; i ++)
            {
                for(int j = 0; j <= n; j ++)
                {
                    if(i == j) gra[i][j] = 0,mon[i][j] = 0;
                    else gra[i][j] = inf,mon[i][j] = inf;
                }
            }
            for(int i = 0; i < m; i ++)
            {
                scanf("%d%d%d%d",&u,&v,&w,&p);
                gra[u][v] = gra[v][u] = w;
                mon[u][v] = mon[v][u] = p;
            }
            dijkstra( s,d, n);
        }
        return 0;
    }
    
    
    
    
  • 相关阅读:
    android中文件操作的四种枚举
    【第4节】索引、视图、触发器、储存过程、
    【第3篇】数据库之增删改查操作
    【第2篇】基本操作和存储引擎
    【第1篇】数据库安装
    123
    111
    1111111
    源码
    【COLLECTION模块】
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139424.html
Copyright © 2020-2023  润新知