CCF CSP 交通规划

吐槽: CCF绝对是最垃圾的OJ,没有之一,我内存超了,从错误类型中没看出来…..

  • 为了防止内存超了,利用临接链表
  • 为了防止时间超了,利用优先队列,其他的比较简答了。
#include <iostream>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <assert.h>
#include <queue>

using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXSIZE = 10005;
struct Edge{
    int b,c;
    Edge(int b,int c):b(b),c(c){}
    bool operator<(Edge const & edge) const {
        return c>edge.c;
    }
};
vector<Edge> road[MAXSIZE];
int n, m;
int sum = 0;
void dij() {
    vector<bool> visit(n, 0);
    vector<int> cost(n, INF);
    vector<int> dis(n, INF);
    priority_queue<Edge> pq;
    dis[0] = 0, cost[0] = 0;
    pq.push(Edge(0,0));
    while (!pq.empty()){
        Edge edge = pq.top();
        pq.pop();
        visit[edge.b] = 1;
        int u = edge.b;
        for (int k = 0; k < road[u].size(); ++k) {
            edge = road[u][k];
            if (visit[edge.b]) continue;
            if (dis[edge.b]>dis[u]+edge.c){
                dis[edge.b] = dis[u]+edge.c;
                cost[edge.b] = edge.c;
                pq.push(Edge(edge.b,dis[edge.b]));
            } else if (dis[edge.b] == dis[u]+edge.c){
                cost[edge.b] = min(edge.c, cost[edge.b]);
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        sum += cost[i];
    }
}
int main() {
//    freopen("../in.txt", "r", stdin);
    cin >> n >> m;
    int a, b, c;
    for (int i = 0; i < m; ++i) {
        cin >> a >> b >> c;
        a--, b--;
        road[a].push_back(Edge(b,c));
        road[b].push_back(Edge(a,c));
    }
    dij();
    cout << sum << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值