zoukankan      html  css  js  c++  java
  • ZOJ 3946 Highway Project (最短路)

    题意:单源最短路,给你一些路,给你这些路的长度,给你修这些路的话费,求最短路和最小花费。

    析:本质就是一个最短路,不过要维护两个值罢了,在维护花费时要维护的是该路要花多少,而不是总的路线花费。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e16;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e5 + 10;
    const int mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    struct Node{
      int v, c, d;
    };
    
    vector<Node> G[maxn];
    struct node{
      int u;  LL d, c;
      node(){ }
      node(int uu, LL dd, LL cc) : u(uu), d(dd), c(cc) { }
      bool operator < (const node &p) const{
        return d > p.d || (d == p.d && c > p.c);
      }
    };
    
    LL d[maxn], c[maxn];
    
    void solve(){
      priority_queue<node> pq;
      pq.push(node(0, 0, 0));
      d[0] = c[0] = 0;
    
      while(!pq.empty()){
        node U = pq.top();  pq.pop();
        int u = U.u;
        for(int i = 0; i < G[u].size(); ++i){
          Node &V = G[u][i];
          int v = V.v;
          if(d[v] > d[u] + V.d){
            d[v] = d[u] + V.d;
            c[v] = V.c;
            pq.push(node(v, d[v], c[v]));
          }
          else if(d[v] == d[u] + V.d && c[v] > V.c){
            c[v] = V.c;
            pq.push(node(v, d[v], c[v]));
          }
        }
      }
      LL ans1 = 0, ans2 = 0;
      for(int i = 1; i < n; ++i){
        ans1 += d[i];
        ans2 += c[i];
      }
    
      printf("%lld %lld
    ", ans1, ans2);
    }
    
    int main(){
      int T;  cin >> T;
      while(T--){
        scanf("%d %d", &n, &m);
        for(int i = 0; i < n; ++i){
          G[i].clear();
          c[i] = d[i] = LNF;
        }
        for(int i = 0; i < m; ++i){
          int x;
          Node u;
          scanf("%d %d %d %d", &x, &u.v, &u.d, &u.c);
          G[x].push_back(u);
          swap(x, u.v);
          G[x].push_back(u);
        }
        solve();
      }
      return 0;
    }
    

      

  • 相关阅读:
    C#中跨线程访问控件问题解决方案
    asp.net网站中配置文件的加密
    C#中XML使用总结
    连接加密Access数据库的字符串设置方法
    asp.net中常用的26个优化性能的方法
    C#中Math的使用总结
    关于ASP.NET页面打印技术的总结
    域登录获取用户名字的控制
    Web界面设计基本原则
    域登录获得用户名称
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6736084.html
Copyright © 2011-2022 走看看