The Shortest Path in Nya Graph HDU - 4725

题目:

This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on. 
The Nya graph is an undirected graph with "layers". Each node in the graph belongs to a layer, there are N nodes in total. 
You can move from any node in layer x to any node in layer x + 1, with cost C, since the roads are bi-directional, moving from layer x + 1 to layer x is also allowed with the same cost. 
Besides, there are M extra edges, each connecting a pair of node u and v, with cost w. 
Help us calculate the shortest path from node 1 to node N.

InputThe first line has a number T (T <= 20) , indicating the number of test cases. 
For each test case, first line has three numbers N, M (0 <= N, M <= 10 5) and C(1 <= C <= 10 3), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers. 
The second line has N numbers l i (1 <= l i <= N), which is the layer of i th node belong to. 
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 10 4), which means there is an extra edge, connecting a pair of node u and v, with cost w.OutputFor test case X, output "Case #X: " first, then output the minimum cost moving from node 1 to node N. 
If there are no solutions, output -1.Sample Input

2
3 3 3
1 3 2
1 2 1
2 3 1
1 3 3

3 3 3
1 3 2
1 2 2
2 3 2
1 3 4

Sample Output

Case #1: 2
Case #2: 3

分析:
主要是构图,m条边正常建,对于相邻层的建图:对于每一层,层点 → 该层每个点,该层每个点→邻层层点(均为单项)
代码:
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 9e5 + 10;
 5 const ll inf = 1e18;
 6 struct edge
 7 {
 8     int e, v, next;
 9     edge(int a = 0, int b = 0, int c = 0) : e(a), v(b), next(c) {}
10 }e[maxn];
11 int head[maxn];
12 ll dis[maxn];
13 int vis[maxn];
14 int v[maxn];
15 int have[maxn];
16 int tot;
17 
18 struct node
19 {
20     int e;
21     ll dis;
22     node(int a = 0, ll b = 0) : e(a), dis(b) {}
23     bool operator< (const node& a) const
24     {
25         return a.dis < dis;
26     }
27 };
28 
29 void add(int x, int y, int v, int tot)
30 {
31     e[tot] = edge(y, v, head[x]);
32     head[x] =tot;
33 }
34 
35 void dj()
36 {
37     for (int i = 1; i < maxn; i++)
38         dis[i] = inf;
39     dis[1] = 0;
40     memset(vis, 0, sizeof(vis));
41     priority_queue<node> q;
42     q.push(node(1, 0));
43 
44 
45     while (!q.empty())
46     {
47         node temp = q.top(); q.pop();
48         int u = temp.e;
49         if (vis[u]) continue;
50         vis[u] = 1;
51         for (int i = head[u]; i != -1; i = e[i].next)
52         {
53             int y = e[i].e;
54             if (!vis[y] && dis[y] > dis[u] + e[i].v)
55             {
56                 dis[y] = dis[u] + e[i].v;
57                 q.push(node(y, dis[y]));
58             }
59         }
60     }
61 }
62 
63 int main()
64 {
65     int T; cin >> T;
66     int n, m, c;
67     int cases = 0;
68 
69     while (T--)
70     {
71         int x, y, w;
72 
73         cin >> n >> m >> c;
74         tot = 0;
75         memset(head, -1, sizeof(head));
76         memset(have, 0, sizeof(have));
77         for (int i = 1; i <= n; i++)//input
78         {
79             scanf("%d", &v[i]);
80             have[v[i]] = 1;
81         }
82         for (int i = 1; i <= n; i++)
83         {
84             add(v[i] + n, i, 0, ++tot);
85             if (v[i] > 1 && have[v[i] - 1]) add(i, v[i] - 1 + n, c, ++tot);
86             if (v[i] < n && have[v[i] + 1]) add(i, v[i] + 1 + n, c, ++tot);
87         }
88         for (int i = 1; i <= m; i++)//point to point
89         {
90             scanf("%d%d%d", &x, &y, &w);
91             add(x, y, w, ++tot);
92             add(y, x, w, ++tot);
93         }
94         dj();
95         printf("Case #%d: %lld\n", ++cases, dis[n] == inf ? -1 : dis[n]);
96     }
97 }

 

 

转载于:https://www.cnblogs.com/liuwenhan/p/11432058.html

GCN (Graph Convolutional Network) Shortest-Path-Master 是一种基于图卷积网络的最短路径算法。最短路径问题是图论中的经典问题,对于给定的图和起始点,找到到达目标点的最短路径。 GCN Shortest-Path-Master 通过应用图卷积神经网络的思想来解决最短路径问题。传统的最短路径算法(如Dijkstra算法或贝尔曼-福特算法)在计算过程中不考虑节点的特征信息,只利用图的拓扑结构。而GCN Shortest-Path-Master 利用了节点的特征信息,将节点的邻居节点信息通过图卷积操作进行聚合,得到节点的新特征表示。 GCN Shortest-Path-Master 的核心思想是,通过图卷积层不断更新节点的特征表示,使得节点的特征表示能够包含更多关于最短路径的信息。在每次迭代中,GCN Shortest-Path-Master 将节点的特征与邻居节点的特征进行聚合,得到节点的新特征表示。在网络的最后一层,通过对所有节点进行分类任务,可以得到每个节点到达目标点的最短路径预测。 相比传统的最短路径算法,GCN Shortest-Path-Master 提供了以下优势: 1. GCN Shortest-Path-Master 能够利用节点的特征,从而更好地表达节点之间的相互作用和联系。 2. GCN Shortest-Path-Master 可以自适应地学习节点的特征表示,而无需人工定义特征。 3. GCN Shortest-Path-Master 可以处理大规模的图结构,在计算效率上具有一定优势。 总之,GCN Shortest-Path-Master 是一种基于图卷积神经网络的最短路径算法,通过利用节点的特征信息,能够更好地解决最短路径问题。它在图结构数据中的应用具有很大潜力,在社交网络分析、推荐系统和物流路径规划等领域都有广泛的应用前景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值