首页 > 其他 > 详细

【最短路】POJ-2387 Til the Cows Come Home

时间:2020-05-21 09:40:11      阅读:45      评论:0      收藏:0      [点我收藏+]

POJ 2387 Til the Cows Come Home

题意:给一个无向图,求从结点n到结点1的最短路

思路:裸的Dijkstra……邻接矩阵的话就在原来有向图的基础上多一次AddEdge操作就完事了

const int INF = 100000+1;
const int maxn = 1000+10;

struct node {
	LL d;
	int u;
	bool operator < (const node& k) const {
		return d > k.d;
	}
};

struct Edge {
	int from, to;
	LL dis;
	Edge(int u,int v,int d):from(u),to(v),dis(d){}
};

int n, m;
vector<Edge> Edges;
vector<int> G[maxn];
bool done[maxn];
LL d[maxn];

void solve(){
	cin >> m >> n;
	memset(done, false,sizeof(done));
	for (int i = 1; i <= n; i++) d[i] = (i == n ? 0 : INF);
	Edges.push_back(Edge(0, 0, 0));
	int num = 0;
	for (int i = 1; i <= m; i++) {
		int u, v, d;
		cin >> u >> v >> d;
		Edges.push_back(Edge(u, v, d));
		G[u].push_back(++num);
		Edges.push_back(Edge(v, u, d));
		G[v].push_back(++num);
	}
	priority_queue<node> Q;
	Q.push(node{ 0, n });
	while (!Q.empty()) {
		node x = Q.top(); Q.pop();
		int u = x.u;
		if (done[u]) continue;
		for (int i = 0; i < G[u].size(); i++) {
			Edge& e = Edges[G[u][i]];
			if (d[u] + e.dis < d[e.to]) {
				d[e.to] = d[u] + e.dis;
				Q.push(node{ d[e.to],e.to });
			}
		}
		done[u] = true;
	}
	cout << d[1];
}

【最短路】POJ-2387 Til the Cows Come Home

原文:https://www.cnblogs.com/streamazure/p/12927528.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!