#include <bits/stdc++.h>
using namespace std;
#define FOR(i,n) for (int i=1;i<=n;i++)
#define REP(i,a,b) for (int i=a;i<=b;i++)
#define pb push_back
#define fi first
#define se second
#define pi pair<int,int>
#define mp make_pair
#define sz(x) ((int)(x).size())
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll linf=1e18;
const int N=100000+10;
const int M=200000+10;
const double eps=1e-5;
const int mo=1e9+7;
int n,k,m;
int a[6];
int x,y,w;
struct edge {
int to,nxt;
ll w;
} e[2*M];
int tot;
int head[N];
ll d[6][N];
int id[6][6];
ll f[11][N];
struct node {
int x;
ll v;
bool operator<(node a) const {
return v>a.v;
}
};
priority_queue<node> pq;
ll ans=linf;
void insert(int x,int y,int w) {
e[++tot].to=y,e[tot].w=w,e[tot].nxt=head[x],head[x]=tot;
e[++tot].to=x,e[tot].w=w,e[tot].nxt=head[y],head[y]=tot;
}
void Dijkstra() {
FOR(i,k) FOR(j,n) d[i][j]=linf;
FOR(i,k) {
d[i][a[i]]=0;
pq.push(node{a[i],0});
node u;
int t,y,w;
while (!pq.empty()) {
u=pq.top();
pq.pop();
if (u.v>d[i][u.x]) continue;
t=u.x;
for (int j=head[t];j;j=e[j].nxt) {
y=e[j].to,w=e[j].w;
if (d[i][t]+w<d[i][y]) {
d[i][y]=d[i][t]+w;
pq.push(node{y,d[i][y]});
}
}
}
}
int cnt=0;
FOR(i,k) REP(j,i+1,k) id[i][j]=++cnt;
FOR(i,10) FOR(j,n) f[i][j]=linf;
FOR(i,k) REP(j,i+1,k) {
int num=id[i][j];
FOR(x,n) if (d[i][x]+d[j][x]==d[i][a[j]]) {
f[num][x]=d[i][a[j]];
pq.push(node{x,f[num][x]});
}
node u;
int t,y,w;
while (!pq.empty()) {
u=pq.top();
pq.pop();
if (u.v>f[num][u.x]) continue;
t=u.x;
for (int j=head[t];j;j=e[j].nxt) {
y=e[j].to,w=e[j].w;
if (f[num][t]+w<f[num][y]) {
f[num][y]=f[num][t]+w;
pq.push(node{y,f[num][y]});
}
}
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//注意你会爆int,你还没改!
scanf("%d %d %d",&n,&k,&m);
FOR(i,k) scanf("%d",&a[i]);
FOR(i,m) {
scanf("%d %d %d",&x,&y,&w);
insert(x,y,w);
}
Dijkstra();
if (k==2) {
ans=d[1][a[2]];
} else if (k==3) {
FOR(i,n) {
ans=min(ans,d[1][i]+d[2][i]+d[3][i]);
}
} else if (k==4) {
ll res=0;
FOR(i,k) REP(j,i+1,k) FOR(x,n) {
res=f[id[i][j]][x];
FOR(l,k) if (l!=i&&l!=j) res+=d[l][x];
ans=min(ans,res);
}
} else {
ll res=0;
int p1=0,p2=0;
FOR(i,k) REP(j,i+1,k) FOR(x,n) {
FOR(l,k) if (l!=i&&l!=j) {
res=f[id[i][j]][x];
p1=p2=0;
FOR(r,k) if (r!=i&&r!=j&&r!=l) {
if (p1) p2=r;
else p1=r;
}
res+=d[l][x];
res+=f[id[p1][p2]][x];
ans=min(ans,res);
}
}
}
printf("%lld\n",ans);
return 0;
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:109:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&n,&k,&m);
~~~~~^~~~~~~~~~~~~~~~~~~~~
cities.cpp:110:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
FOR(i,k) scanf("%d",&a[i]);
~~~~~^~~~~~~~~~~~
cities.cpp:112:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&x,&y,&w);
~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
628 KB |
Output is correct |
3 |
Correct |
2 ms |
712 KB |
Output is correct |
4 |
Correct |
2 ms |
712 KB |
Output is correct |
5 |
Correct |
2 ms |
712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
681 ms |
20240 KB |
Output is correct |
2 |
Correct |
625 ms |
24960 KB |
Output is correct |
3 |
Correct |
148 ms |
24960 KB |
Output is correct |
4 |
Correct |
274 ms |
24960 KB |
Output is correct |
5 |
Correct |
357 ms |
33684 KB |
Output is correct |
6 |
Correct |
166 ms |
33684 KB |
Output is correct |
7 |
Correct |
4 ms |
33684 KB |
Output is correct |
8 |
Correct |
3 ms |
33684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
33684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
990 ms |
40672 KB |
Output is correct |
2 |
Correct |
969 ms |
45328 KB |
Output is correct |
3 |
Correct |
249 ms |
45328 KB |
Output is correct |
4 |
Correct |
736 ms |
45528 KB |
Output is correct |
5 |
Correct |
377 ms |
45528 KB |
Output is correct |
6 |
Correct |
287 ms |
46648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1486 ms |
61180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |