Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a9430c2

Browse files
committed
Add Solution for shortestpath1 [OpenKattis]
1 parent 48e25ae commit a9430c2

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

‎OpenKattis/shortestpath1/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<img src="problem.png" />

‎OpenKattis/shortestpath1/problem.png

170 KB
Loading[フレーム]

‎OpenKattis/shortestpath1/solution.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
import io
3+
import os
4+
5+
from collections import defaultdict
6+
from queue import PriorityQueue
7+
8+
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
9+
10+
while True:
11+
n, m, q, s = map(int, input().split())
12+
if (n + m + q + s) == 0:
13+
break
14+
arr = defaultdict(lambda: [])
15+
for i in range(m):
16+
u, v, w = map(int, input().split())
17+
arr[u].append((w, v))
18+
19+
distance = defaultdict(lambda: 2e9)
20+
visited = defaultdict(lambda: False)
21+
short_path = PriorityQueue(maxsize=m)
22+
23+
distance[s] = 0;
24+
short_path.put((0, s))
25+
26+
while not short_path.empty():
27+
node = short_path.get()[1]
28+
29+
if visited[node]: continue
30+
visited[node] = True
31+
32+
for w, n in arr[node]:
33+
if distance[n] > w + distance[node]:
34+
distance[n] = w + distance[node]
35+
short_path.put((distance[n], n))
36+
37+
for i in range(q):
38+
query = int(input())
39+
if distance[query] == 2e9:
40+
sys.stdout.write("Impossible\n")
41+
sys.stdout.flush()
42+
else:
43+
sys.stdout.write(str(distance[query]) + '\n')
44+
sys.stdout.flush()
45+
sys.stdout.write('\n')
46+
sys.stdout.flush()

‎OpenKattis/shortestpath1/soution.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include<bits/stdc++.h>
2+
#define INF 99999999
3+
4+
using namespace std;
5+
6+
struct edge
7+
{
8+
int first;
9+
int second;
10+
edge(){}
11+
edge(int f, int s){
12+
first = f;
13+
second = s;
14+
}
15+
bool operator<(edge e) const{
16+
return first > e.first;
17+
}
18+
};
19+
20+
bitset<10000+9> visited;
21+
int main(){
22+
ios_base::sync_with_stdio(false);
23+
cin.tie(NULL);
24+
int n, m, q, s, u, v, w;
25+
while(cin >> n >> m >> q >> s){
26+
if ((n + m + q + s) == 0)
27+
break;
28+
29+
vector<edge> arr[n+1];
30+
31+
for(int i = 0; i < m; i++){
32+
cin >> u >> v >> w;
33+
arr[u].push_back(edge(w, v));
34+
}
35+
vector<int> dist(n+9, INF);
36+
dist[s] = 0;
37+
38+
priority_queue<edge> short_path;
39+
40+
visited.reset();
41+
short_path.push(edge(0, s));
42+
43+
while(!short_path.empty()){
44+
edge data = short_path.top();
45+
short_path.pop();
46+
47+
int node = data.second;
48+
49+
if (visited[node]) continue;
50+
visited.set(node);
51+
52+
for(auto p: arr[node]) {
53+
if (dist[p.second] > (dist[node] + p.first)){
54+
dist[p.second] = dist[node] + p.first;
55+
short_path.push(edge(dist[p.second], p.second));
56+
}
57+
}
58+
}
59+
int in;
60+
for (int i = 0; i < q; i++){
61+
cin >> in;
62+
if(dist[in] == INF)
63+
cout << "Impossible\n";
64+
else
65+
cout << dist[in] << "\n";
66+
}
67+
cout << "\n";
68+
}
69+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /