Submission #82202

# Submission time Handle Problem Language Result Execution time Memory
82202 2018-10-29T13:23:06 Z AngelKnows Cities (BOI16_cities) C++14
14 / 100
6000 ms 20056 KB
#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) {
		if (n<=999) while (1);
		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);
      ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 612 KB Output is correct
3 Correct 2 ms 612 KB Output is correct
4 Execution timed out 6090 ms 620 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 585 ms 18500 KB Output is correct
2 Correct 555 ms 18964 KB Output is correct
3 Correct 141 ms 18964 KB Output is correct
4 Correct 169 ms 18964 KB Output is correct
5 Correct 338 ms 18964 KB Output is correct
6 Correct 157 ms 18964 KB Output is correct
7 Correct 4 ms 18964 KB Output is correct
8 Correct 4 ms 18964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 18964 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 807 ms 19292 KB Output is correct
2 Correct 862 ms 19792 KB Output is correct
3 Correct 187 ms 19792 KB Output is correct
4 Correct 701 ms 19792 KB Output is correct
5 Correct 361 ms 19792 KB Output is correct
6 Execution timed out 6073 ms 19792 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Incorrect 1298 ms 20056 KB Output isn't correct
2 Halted 0 ms 0 KB -