#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 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*N];
int tot;
int head[N];
bool vis[N];
ll d[6][N];
int id[6][6];
ll f[11][N];
priority_queue<ll,vector<ll>,greater<ll>> 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) {
memset(vis,0,sizeof vis);
d[i][a[i]]=0;
pq.push(a[i]);
int t,y,w;
while (!pq.empty()) {
t=pq.top();
pq.pop();
if (vis[t]) continue;
vis[t]=1;
for (int j=head[t];j;j=e[j].nxt) {
y=e[j].to,w=e[j].w;
if (!vis[y]&&d[i][t]+w<d[i][y]) {
d[i][y]=d[i][t]+w;
pq.push(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) {
memset(vis,0,sizeof vis);
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(x);
}
int t,y,w;
while (!pq.empty()) {
t=pq.top();
pq.pop();
if (vis[t]) continue;
vis[t]=1;
for (int j=head[t];j;j=e[j].nxt) {
y=e[j].to,w=e[j].w;
if (!vis[y]&&f[num][t]+w<f[num][y]) {
f[num][y]=f[num][t]+w;
pq.push(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("%d\n",ans);
return 0;
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:139:19: warning: format '%d' expects argument of type 'int', but argument 2 has type 'll {aka long long int}' [-Wformat=]
printf("%d\n",ans);
^
cities.cpp:102: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:103: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:105: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);
~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
17 ms |
640 KB |
Output is correct |
3 |
Incorrect |
2 ms |
708 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
52 ms |
6348 KB |
Time limit exceeded (wall clock) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
6348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
54 ms |
8600 KB |
Time limit exceeded (wall clock) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
55 ms |
11016 KB |
Time limit exceeded (wall clock) |
2 |
Halted |
0 ms |
0 KB |
- |