201609-4-----------交通规划

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int N = 10010, M = 100010, INF = 0x3f3f3f3f;
typedef pair<int, int> PII;
int h[N], e[M], ne[M], w[M], idx;
bool st[N];
int dist[N];
int cost[N];
void add(int a, int b, int c){
	e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}
void dijkstra(int p){
	priority_queue<PII, vector<PII>, greater<PII>> heap;
	dist[p] = 0;
	cost[p] = 0;
	heap.push({dist[p], p});
		while(heap.size()){
		auto t = heap.top();
		heap.pop();
		int u = t.second;
				//cout << t.first << " " << t.second << endl;
				if (st[u])   continue;
		st[u] = true;
				for (int i = h[u]; ~i; i = ne[i]){
			int j = e[i], l = w[i];
		//	cout << l << endl;
			if (st[j])   continue;
			if (dist[u] + l < dist[j]){
				dist[j] = dist[u] + l;
				cost[j] = l;
				heap.push({dist[j], j});
			}
			else if (dist[u] + l == dist[j]){
				cost[j] = min(cost[j], l);
			}
		}
	}
}
int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	memset(h, -1, sizeof h);
	while(m --){
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		add(a, b, c), add(b, a, c);
	}
		memset(st, 0, sizeof st);
	memset(dist, 0x3f, sizeof dist);
	memset(cost, 0x3f, sizeof cost);
		dijkstra(1);
	//	cout << "-------" << endl;
		int ans = 0;
	for (int i = 1; i <= n; i ++)   ans += cost[i];
		cout << ans << endl;
		return 0;
}

80分以后改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值