AOJ 2249 Road Construction

终于写对了。。。

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 1000000000;
struct node{
    int x, dis, cost;
};
int n, m, c1, c2, c3, c4, d[10010], pre[10010];
vector<node> v[10010];
bool visit[10010];
void Dijkstra(int s)
{
    fill(d, d + 10010, INF);
    fill(pre, pre + 10010, INF);
    fill(visit, visit + 10010, false);
    d[s] = pre[s] = 0;
    for(int i = 0; i < n; i++){
        int u = -1, min = INF;
        for(int j = 1; j <= n; j++){
            if(!visit[j] && d[j] < min){
                u = j;
                min = d[j];
            }
        }
        if(u == -1) return;
        visit[u] = true;
        for(int j = 0; j < v[u].size(); j++){
            node temp = v[u][j];
            if(!visit[temp.x]){
                if(d[temp.x] > d[u] + temp.dis){
                    d[temp.x] = d[u] + temp.dis;
                    pre[temp.x] = temp.cost;
                }else if(d[temp.x] == d[u] + temp.dis && pre[temp.x] > temp.cost) pre[temp.x] = temp.cost;
            }
        }
    }
}
int main()
{
    while(scanf("%d%d", &n, &m) != EOF){
        if(!n && !m) break;
        int sum = 0;
        for(int i = 0; i < 10010; i++){
            v[i].clear();
        }
        for(int i = 0; i < m; i++){
            scanf("%d%d%d%d", &c1, &c2, &c3, &c4);
            node temp;
            temp.x = c2, temp.dis = c3, temp.cost = c4;
            v[c1].push_back(temp);
            temp.x = c1;
            v[c2].push_back(temp);
        }
        Dijkstra(1);
        for(int i = 2; i <= n; i++){
            sum += pre[i];
        }
        printf("%d\n", sum);
    }
    return 0;
}
发布了29 篇原创文章 · 获赞 0 · 访问量 360

猜你喜欢

转载自blog.csdn.net/qq_33942309/article/details/105698293