当前位置:网站首页>HDU3873-有依赖的最短路(拓扑排序)
HDU3873-有依赖的最短路(拓扑排序)
2022-07-25 15:23:00 【塔子哥来了】
题目大意:
给一张图,跑最短路。但是每个点可能有前驱点集,即必须要先访问到前驱点,才能访问这个点.
题目思路:
可以发现,在这种限定条件下。我们访问节点的顺序一定是一个拓扑序.
所以思路是一边Dij一边拓扑排序.能够入列,当前仅当它的入度为0.
然后对着样例讨论一下, 竟 然 过 了
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define fi first
#define se second
const int maxn = 3000 + 5;
const int mod = 1e9 + 7;
int du[maxn] , bk[maxn];
ll dist[maxn];
vector<pair<int,ll>> e[maxn];
vector<int> to[maxn];
struct Node
{
int id;
ll v;
bool operator < (const Node & a) const {
return v > a.v;
}
};
void init (int n){
for (int i = 1 ; i <= n ; i++){
to[i].clear();
e[i].clear();
dist[i] = -1;
du[i] = bk[i] = 0;
}
}
int main()
{
ios::sync_with_stdio(false);
int t; cin >> t;
while (t--){
int n , m; cin >> n >> m;
init(n);
for (int i = 1 ; i <= m ; i++){
int x , y , z; cin >> x >> y >> z;
e[x].push_back({
y , z});
}
for (int i = 1 ; i <= n ; i++){
int d; cin >> d;
for (int j = 1 ; j <= d ; j++){
int g; cin >> g;
to[g].push_back(i);
}
du[i] = d;
}
priority_queue<Node> q;
q.push({
1 , 0});
dist[1] = 0;
while (q.size()){
Node u = q.top(); q.pop();
int id = u.id;
ll dis = u.v;
// cout << "现在在" << id << " 距离是 " << dis << endl;
if (bk[id]) continue;
bk[id] = 1;
for (auto & v : to[id]) {
du[v]--;
if (du[v] == 0){
// 如果dist[v] == -1, 那么dis 不可能是v的最大前驱值
if (dist[v] != -1){
dist[v] = max(dist[v] , dis);
q.push({
v , dist[v]});
}
}
}
for (auto & g : e[id]){
int v = g.first;
ll w = g.second;
if (dist[v] == -1 || dist[v] > dis + w){
dist[v] = dis + w;
if (!du[v]) q.push({
v , dist[v]});
}
}
}
cout << dist[n] << endl;
}
return 0;
}
边栏推荐
- Singleton mode 3-- singleton mode
- JVM parameter configuration details
- Recommend 10 learning websites that can be called artifact
- 带你详细认识JS基础语法(建议收藏)
- 死锁杂谈
- 请问seata中mysql参数每个客户端连接最大的错误允许数量要怎么理解呢?
- Example of password strength verification
- Once spark reported an error: failed to allocate a page (67108864 bytes), try again
- 数据系统分区设计 - 分区与二级索引
- Spark002 --- spark task submission, pass JSON as a parameter
猜你喜欢

分布式原理 - 什么是分布式系统

为什么PrepareStatement性能更好更安全?

Solve the timeout of dbeaver SQL client connection Phoenix query

《图书馆管理系统——“借书还书”模块》项目研发阶段性总结

ML - natural language processing - Basics

获取键盘按下的键位对应ask码

图论及概念

No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac

Spark SQL空值Null,NaN判断和处理

Example of password strength verification
随机推荐
Image cropper example
Distributed principle - what is a distributed system
Spark memory management mechanism new version
Idea远程提交spark任务到yarn集群
pageHelper不生效,sql没有自动加上limit
数据系统分区设计 - 分区再平衡(rebalancing)
Spark SQL空值Null,NaN判断和处理
Promise object and macro task, micro task
死锁杂谈
Solve the timeout of dbeaver SQL client connection Phoenix query
UITextField的inputView和inputAccessoryView注意点
小波变换--dwt2 与wavedec2
spark中saveAsTextFile如何最终生成一个文件
Simulate setinterval timer with setTimeout
Maxcompute SQL 的查询结果条数受限1W
How to understand the maximum allowable number of errors per client connection of MySQL parameters in Seata?
redis淘汰策列
JVM-垃圾收集器详解
解决DBeaver SQL Client 连接phoenix查询超时
如何更新更新数据库中的json值?