This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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]]) {
FOR(x,n) {
f[num][x]=d[i][x]+d[j][x];
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];
int tt=0,tt2=0;
FOR(l,k) if (l!=i&&l!=j) {
res+=d[l][x];
if (tt) tt2=d[l][x];
else tt=d[l][x];
}
//cout<<i<<" "<<j<<" "<<x<<" "<<f[id[i][j]][x]<<" "<<tt<<" "<<tt2<<" "<<res<<endl;
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 (stderr)
cities.cpp: In function 'int main()':
cities.cpp:128:13: warning: variable 'tt2' set but not used [-Wunused-but-set-variable]
int tt=0,tt2=0;
^~~
cities.cpp:111: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:112: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:114: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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |