Login
Register
Problem list
Online status
Huhu_Miao
:
2024-11-17 01:22:31
/** * author: Huhu_Miao * created: 2024.11.17 01:21:00 (UTC+8) **/ #include
using pii = std::pair
; void solve(){ int n; std::cin >> n; std::vector
maxDistance(n+1,0); std::queue
que; std::vector
> graph(n+1,std::vector
(0)); for(int i = 1 ; i < n + 1 ; i++){ for(int j = 1 ; j < n + 1 ; j++){ int w; std::cin >> w; if(w != -1) graph[i].emplace_back(j,w); } } que.push(1); while(!que.empty()){ int u = que.front(); que.pop(); for(const auto & [v,w]:graph[u]){ if(maxDistance[u] + w > maxDistance[v]){ maxDistance[v] = maxDistance[u]+w; que.push(v); } } } std::cout << maxDistance[n] << '\n'; } int main(){ std::ios::sync_with_stdio(false);std::cin.tie(nullptr); int T; std::cin>>T; while(T--) solve(); return 0; }
09023310
:
2024-10-08 20:20:27
BFS求最大长度路径 #include
#include
#include
using namespace std; bool isValid(vector
> map, int i, int j) { if (i < 0 || j < 0 || i > map.size() || j > map[0].size() || map[i][j] == -1) { return false; } return true; } int leaveTime(vector
> map, int n) { vector
distance(n, -1); // 从0点出发到各点距离,初始化为-1 distance[0] = 0; queue
que = {}; vector
in_degree(n, 0); // 计算入度 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (isValid(map, i, j)) { in_degree[j]++; } } } // 将入度为 0 的点设为起始点 for (int j = 0; j < n; j++) { if (!in_degree[j]) { que.push(j); } } // 广度优先搜索 while (!que.empty()) { int temp = que.front(); que.pop(); for (int j = 0; j < n; j++) { if (isValid(map, temp, j)) { distance[j] = max(distance[j], distance[temp] + map[temp][j]); // 计算较大距离 in_degree[j]--; if (!in_degree[j]) { que.push(j); // 如果入度为0,加入队列 } } } } return distance[n - 1]; } int main() { int T; cin >> T; vector
ans = {}; while (T--) { int n; cin >> n; vector
> map(n, vector
(n, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int temp; cin >> temp; map[i][j] = temp; } } ans.push_back(leaveTime(map, n)); } for (int i : ans) { cout << i << endl; } }
RockyChen0205
:
2023-12-15 12:11:43
AOE图的关键路径,代码如下: #include
#include
#include
using namespace std; int main(){ int T=0; //t行 cin>>T; for(int t=0;t
>n; vector
> map(n,vector
(n,0)); vector
count(n,0); //拓扑排序过程中使用 vector
ee(n,0); //时间最早发生时间 for(int i=0;i
>map[i][j]; if(map[i][j]!=-1) count[j]++; //数有j这个节点有多少个前驱结点 } } queue
q; vector
top; q.push(0); //拓扑排序 while(!q.empty()){ int p=q.front(); q.pop(); top.push_back(p); for(int i=0;i
ee[i]){ ee[i]=a+length; } } } } cout<
Post Your Comment