P1608的讨论(路径统计)
  • 板块题目总版
  • 楼主sjzwer_
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/9/24 21:25
  • 上次更新2024/9/25 10:03:50
查看原帖
P1608的讨论(路径统计)
1097868
sjzwer_楼主2024/9/24 21:25

P1608

好难啊,谁能过我点提示???

#include<bits/stdc++.h>
using namespace std;
int n , m , ans = 0;
int u , v , w;
int dist[2010];
bool vis[2010];
struct Node{
	int v , w;
	bool operator<(Node node)const{
		return w < node.w;
	}
};
priority_queue<Node> q;
vector<Node> g[2010];
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	memset(dist , 0x3f3f3f3f , sizeof dist);
	cin >> n >> m;
	dist[1] = 0;
	for(int i = 1; i <= m ; i++){
		cin >> u >> v >> w;
		g[u].push_back(Node{v , w});
	}
	q.push(Node{1 , 0});
	while(!q.empty()){
		Node head = q.top();
		q.pop();
		if(vis[head.v] == true) continue;
		vis[head.v] = true;
		for(Node node : g[head.v]){
			if(dist[head.v]+node.w<dist[node.v]){
				dist[node.v]=dist[head.v]+node.w;
				ans++;
			}
			q.push(Node{node.v,dist[node.v]});
		}
	}
	if(dist[n] == 0x3f3f3f3f) {
		cout << "No answer";
		return 0;
	}
	cout << dist[n] << " " << ans - 1;
	return 0;
}
//太难了!!!P1608
//谁能在我的代码基础上给点计数的方法,谢谢; 
2024/9/24 21:25
加载中...