using System using System Collections Generic using System Linq using

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace DejkstraWithStrangeStructure
{
class Program
{
static void Main(string[] args)
{
//StreamReader sr = new StreamReader("input.txt");
//StreamWriter sw = new StreamWriter("output.txt");
Func<string> getLine = Console.ReadLine;
Action<string> putLine = (string p) => Console.WriteLine(p);
Int32[] tmp; char[] seps = new char[] { ' ' };
tmp = getLine().Split(seps, StringSplitOptions.RemoveEmptyEntries).Select(p => Int32.Parse(p)).ToArray();
Int32 n = tmp[0];
//Int32 m = tmp[1];
Int32 start = tmp[1] - 1;
Int32 finish = tmp[2] - 1;
List<List<Tuple<Int32, Int32>>> g = new List<List<Tuple<Int32, Int32>>>(n);
for (Int32 i = 0; i < n; ++i)
g.Add(new List<Tuple<Int32, Int32> >());
//for (Int32 i = 0; i < m; ++i)
//{
// tmp = getLine().Split().Select(p => Int32.Parse(p)).ToArray();
// Int32 a, b, w;
// a = tmp[0] - 1;
// b = tmp[1] - 1;
// w = tmp[2];
// g[a].Add(new Tuple<Int32, Int32>(b, w));
// g[b].Add(new Tuple<Int32, Int32>(a, w));
//}
for (Int32 i = 0; i < n; ++i)
{
tmp = getLine().Split(seps, StringSplitOptions.RemoveEmptyEntries).Select(p => Int32.Parse(p)).ToArray();
for (Int32 j = 0; j < n; ++j)
if (tmp[j] >= 0 && i != j)
g[i].Add(new Tuple<Int32, Int32>(j, tmp[j]));
}
Int32[] dist;
Int32[] pred;
Dejkstra(g, start, out dist, out pred);
if (dist[finish] == Int32.MaxValue) dist[finish] = -1;
//if (dist[finish] >= Int32.MaxValue / 2) throw new Exception("=(");
putLine(dist[finish].ToString());
}
//Tuple<Int32, Int32> - pair destination/weight
public static void Dejkstra(List<List<Tuple<Int32, Int32> > > g, Int32 start, out Int32[] dist, out Int32[] pred)
{
Int32 n = g.Count;
Int32 maxDist = 0;
for (Int32 i = 0; i < n; ++i)
foreach (Tuple<Int32, Int32> p in g[i])
maxDist = Math.Max(maxDist, p.Item2);
dist = new Int32[n];
pred = new Int32[n];
for (Int32 i = 0; i < n; ++i)
{
dist[i] = Int32.MaxValue;
pred[i] = -1;
}
dist[start] = 0;
int cherpacscnt = 10 * maxDist + 1;
StrangeClass cherpaks = new StrangeClass(n, cherpacscnt);
cherpaks.PutNodeToCherpackEnd(start, 0);
Int32 curCherpack = 0;
Int32 zeroIters = 0;
while (zeroIters <= maxDist + 1)
{
if (cherpaks.cherpaksFirst[curCherpack] == null)
{
++zeroIters;
}
else
{
StrangeClass.Node cur = cherpaks.cherpaksFirst[curCherpack];
while (cur != null)
{
Int32 v = cur.vertex;
cherpaks.EraseVertexFromCherpak(v);
foreach(Tuple<Int32, Int32> p in g[v])
{
Int32 u = p.Item1;
Int32 w = p.Item2;
if (dist[v] + w < dist[u])
{
pred[u] = v;
dist[u] = dist[v] + w;
if (dist[u] >= Int32.MaxValue / 2) throw new Exception("=(");
cherpaks.PutNodeToCherpackEnd(u, dist[u]);
}
}
cur = cherpaks.cherpaksFirst[curCherpack];
}
zeroIters = 0;
}
curCherpack = (curCherpack + 1) % cherpacscnt;
}
}
}
class StrangeClass
{
public readonly Int32 vertexCount;
public readonly Int32 cherpaksCount;
public readonly Node[] nodes;
public readonly Node[] cherpaksFirst;
public readonly Node[] cherpaksLast;
private readonly Int32[] nodeIdToCherpakId;
public void EraseVertexFromCherpak(Int32 vertex)
{
Int32 chId = nodeIdToCherpakId[vertex];
if (chId < 0) return;
if (nodes[vertex].next == null)
{
cherpaksLast[chId] = nodes[vertex].prev;
}
else
{
nodes[vertex].next.prev = nodes[vertex].prev;
}
if (nodes[vertex].prev == null)
{
cherpaksFirst[chId] = nodes[vertex].next;
}
else
{
nodes[vertex].prev.next = nodes[vertex].next;
}
nodeIdToCherpakId[vertex] = -1;
nodes[vertex].next = null;
nodes[vertex].prev = null;
}
public void PutNodeToCherpackEnd(Int32 vertex, Int32 cherpack)
{
EraseVertexFromCherpak(vertex);
cherpack %= cherpaksCount;
nodes[vertex].next = null;
nodes[vertex].prev = cherpaksLast[cherpack];
if (nodes[vertex].prev == null)
cherpaksFirst[cherpack] = nodes[vertex];
else
nodes[vertex].prev.next = nodes[vertex];
cherpaksLast[cherpack] = nodes[vertex];
nodeIdToCherpakId[vertex] = cherpack;
}
public StrangeClass(Int32 vertexCount, Int32 cherpaksCount)
{
this.cherpaksCount = cherpaksCount;
this.vertexCount = vertexCount;
nodes = new Node[vertexCount];
nodeIdToCherpakId = new Int32[vertexCount];
cherpaksFirst = new Node[cherpaksCount];
cherpaksLast = new Node[cherpaksCount];
for (Int32 i = 0; i < vertexCount; ++i)
{
nodes[i] = new Node(i);
nodeIdToCherpakId[i] = -1;
}
for (Int32 i = 0; i < cherpaksCount; ++i)
{
cherpaksFirst[i] = cherpaksLast[i] = null;
}
}
public class Node
{
public Node next;
public Node prev;
public readonly Int32 vertex;
public Node(Int32 vertex)
{
next = null;
prev = null;
this.vertex = vertex;
}
}
}
}