zoukankan      html  css  js  c++  java
  • poj 1724 ROADS 最短路

    题目链接

    n个节点, m条边, 一开始有K这么多的钱, 每条边有len, cost两个属性, 求1到n的最短距离, 花费要小于k。

     dis数组开成二维的, dis[u][cost]表示到达u花费为cost的最短路径, 然后dij+堆优化。

    路是单向的..

     1 #include <iostream>
     2 #include <vector>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <algorithm>
     6 #include <cmath>
     7 #include <map>
     8 #include <set>
     9 #include <string>
    10 #include <queue>
    11 using namespace std;
    12 #define pb(x) push_back(x)
    13 #define ll long long
    14 #define mk(x, y) make_pair(x, y)
    15 #define lson l, m, rt<<1
    16 #define mem(a) memset(a, 0, sizeof(a))
    17 #define rson m+1, r, rt<<1|1
    18 #define mem1(a) memset(a, -1, sizeof(a))
    19 #define mem2(a) memset(a, 0x3f, sizeof(a))
    20 #define rep(i, a, n) for(int i = a; i<n; i++)
    21 #define ull unsigned long long
    22 typedef pair<int, int> pll;
    23 const double PI = acos(-1.0);
    24 const double eps = 1e-8;
    25 const int mod = 1e9+7;
    26 const int inf = 1061109567;
    27 const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    28 const int maxn = 1e5+5;
    29 int head[maxn], k, n, num, dis[105][105];
    30 struct node
    31 {
    32     int to, nextt, cost, d;
    33 }e[maxn*2];
    34 void init() {
    35     num = 0;
    36     mem1(head);
    37 }
    38 void add(int u, int v, int cost, int d) {
    39     e[num].to = v;
    40     e[num].nextt = head[u];
    41     e[num].cost = cost;
    42     e[num].d = d;
    43     head[u] = num++;
    44 }
    45 struct ee
    46 {
    47     int u, d, cost;
    48     bool operator < (ee a)const
    49     {
    50         if(d == a.d)
    51             return cost>a.cost;
    52         return d>a.d;
    53     }
    54     ee(){}
    55     ee(int u, int d, int cost):u(u), d(d), cost(cost){}
    56 };
    57 int dij() {
    58     priority_queue <ee> q;
    59     mem2(dis);
    60     dis[1][0] = 0;
    61     q.push(ee(1, 0, 0));
    62     while(!q.empty()) {
    63         ee tmp = q.top(); q.pop();
    64         if(tmp.u == n)
    65             return tmp.d;
    66         for(int i = head[tmp.u]; ~i; i = e[i].nextt) {
    67             int v = e[i].to;
    68             if(dis[v][tmp.cost+e[i].cost]>tmp.d+e[i].d) {
    69                 if(tmp.cost+e[i].cost<=k) {
    70                     q.push(ee(v, tmp.d+e[i].d, tmp.cost+e[i].cost));
    71                     dis[v][tmp.cost+e[i].cost] = tmp.d+e[i].d;
    72                 }
    73             }
    74         }
    75     }
    76     return -1;
    77 }
    78 int main()
    79 {
    80     int m, u, v, w, c;
    81     while(cin>>k) {
    82         cin>>n>>m;
    83         init();
    84         while(m--) {
    85             scanf("%d%d%d%d", &u, &v, &w, &c);
    86             add(u, v, c, w);
    87         }
    88         int ans = dij();
    89         printf("%d
    ", ans);
    90     }
    91     return 0;
    92 }
  • 相关阅读:
    Django虚拟环境创建问题virtualenvwrapper.sh: There was a problem running the initialization hooks.
    面向对象
    使用python中ssh连接CentOS7上的数据库
    Mysql在python中的使用:pymysql
    Mysql索引原理
    Mysql数据记录操作
    Mysql完整性约束
    Mysql支持的数据类型
    Mysql账户设置_增删改查_表操作
    自己项目列表
  • 原文地址:https://www.cnblogs.com/yohaha/p/5055113.html
Copyright © 2011-2022 走看看