Til the Cows Come Home

版子题:
##代码如下:

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<functional>
using namespace std;
typedef pair<int, int> pii;
const int inf = 2e9;
const int max_n = 1100;
const int max_m = 2100;
int m, n;
struct edge{
    int to, cost, next;
}E[max_m << 1];
int head[max_n];
int cnt = 1;
void add(int from, int to, int cost) {
    E[cnt].to = to;
    E[cnt].cost = cost;
    E[cnt].next = head[from];
    head[from] = cnt++;
}
int d[max_n];
int dijstra(int s,int t) {
    fill(d, d + max_n, inf);d[s] = 0;
    priority_queue<pii, vector<pii>, greater<pii>> que;
    que.push(make_pair(d[s], s));
    while (!que.empty()) {
        pii p = que.top();que.pop();
        int u = p.second;int dist = p.first;
        if (dist < d[u])continue;
        for (int i = head[u];i;i=E[i].next) {
            edge& e = E[i];
            if (d[e.to] <= d[u] + e.cost)continue;
            d[e.to] = d[u] + e.cost;
            que.push(make_pair(d[e.to], e.to));
        }
    }return d[t];
}

int main() {
    scanf("%d %d", &m, &n);
    for (int i = 1, u, v, c;i <= m;++i) {
        scanf("%d %d %d", &u, &v, &c);
        add(u, v, c);add(v, u, c);
    }printf("%d\n", dijstra(1, n));
}

刷kuangbin大佬的题单,变强。。。。。。

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务