#include <bits/stdc++.h>
#define DIMN 100010
#define INF 1000000000000000
using namespace std;
long long dist[64][DIMN];
int sp[DIMN] , poz[DIMN];
vector <pair <int,int> > v[DIMN];
priority_queue <pair <long long , pair <int,int> > > h;
char buff[6000000];
int pos = 0;
int getnr(){
int nr = 0;
while ('0' > buff[pos] || buff[pos] > '9')
pos++;
while ('0' <= buff[pos] && buff[pos] <= '9'){
nr = nr * 10 + buff[pos] - '0';
pos++;
}
return nr;
}
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
int n , m , k , i , j , x , y , nod , vecin , c , cst , mask , st , big;
long long sum = 0 , cost;
fread (buff , 1 , 6000000 , fin);
n = getnr();
k = getnr();
m = getnr();
for (i = 1 ; i <= k ; ++i){
sp[i] = getnr();
poz[sp[i]] = i;
}
for (i = 1 ; i <= m ; ++i){
x = getnr();
y = getnr();
c = getnr();
v[x].push_back(make_pair(y , c));
v[y].push_back(make_pair(x , c));
}
for (i = 0 ; i < 32 ; ++i){
for (j = 1 ; j <= n ; ++j)
dist[i][j] = INF;
}
for (j = 1 ; j <= n ; ++j){
if (poz[j]){
dist[1 << (poz[j] - 1)][j] = 0;
h.push(make_pair(0 , make_pair(1 << (poz[j] - 1) , j)));
}
else {
dist[0][j] = 0;
h.push(make_pair(0 , make_pair(0 , j)));
}
}
while (!h.empty()){
cost = -h.top().first;
mask = h.top().second.first;
nod = h.top().second.second;
h.pop();
if (dist[mask][nod] != cost)
continue; /// nu e ok
/// acuma trb sa vad cum se pot combina starile
for (i = 0 ; i < v[nod].size() ; ++i){
vecin = v[nod][i].first;
cst = v[nod][i].second;
big = ((1 << k) - 1) ^ mask;
for (st = big ; ; st = (st - 1) & big ){
if (dist[mask | st][vecin] > dist[mask][nod] + cst + dist[st][vecin]){
dist[mask | st][vecin] = dist[mask][nod] + cst + dist[st][vecin];
h.push(make_pair(-dist[mask | st][vecin] , make_pair(mask|st , vecin)));
}
if (!st)
break;
}
}
}
sum = INF;
for (i = 1 ; i <= n ; ++i)
sum = min(sum , dist[(1 << k) - 1][i]);
fprintf (fout,"%lld",sum);
return 0;
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:82:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0 ; i < v[nod].size() ; ++i){
~~^~~~~~~~~~~~~~~
cities.cpp:35:11: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
fread (buff , 1 , 6000000 , fin);
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2816 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2816 KB |
Output is correct |
4 |
Correct |
6 ms |
2944 KB |
Output is correct |
5 |
Correct |
6 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
998 ms |
54808 KB |
Output is correct |
2 |
Correct |
930 ms |
54476 KB |
Output is correct |
3 |
Correct |
416 ms |
35184 KB |
Output is correct |
4 |
Correct |
60 ms |
12028 KB |
Output is correct |
5 |
Correct |
330 ms |
42464 KB |
Output is correct |
6 |
Correct |
33 ms |
11516 KB |
Output is correct |
7 |
Correct |
11 ms |
3456 KB |
Output is correct |
8 |
Correct |
8 ms |
3328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
3840 KB |
Output is correct |
2 |
Correct |
14 ms |
3840 KB |
Output is correct |
3 |
Correct |
10 ms |
3328 KB |
Output is correct |
4 |
Correct |
12 ms |
3712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3286 ms |
104040 KB |
Output is correct |
2 |
Correct |
2998 ms |
103572 KB |
Output is correct |
3 |
Correct |
1679 ms |
49616 KB |
Output is correct |
4 |
Correct |
1991 ms |
58300 KB |
Output is correct |
5 |
Correct |
382 ms |
30428 KB |
Output is correct |
6 |
Correct |
125 ms |
14848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6082 ms |
169808 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |