| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
为何vector存图TLE#include <cstring>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define qq 70010
#define pb push_back
#define REP(i, x, n) for(int i = x; i < n; ++i)
vector<int> G[qq], dist[qq], u[qq], f[qq];
bool vis[qq];
int anc[qq], w[qq], fa[qq];
int n, m;
int Find(int x){
return fa[x] == -1 ? x : fa[x] = Find(fa[x]);
}
void Unoin(int x, int y){
int b = Find(y);
if(b != x) fa[b] = x;
}
void Tarjan(int x, int c){
anc[x] = c;
vis[x] = true;
fa[x] = -1;
int sz = (int)G[x].size();
REP(i, 0, sz)if(!vis[G[x][i]]){
int y = G[x][i];
Tarjan(y, c + dist[x][i]);
Unoin(x, y);
}
sz = (int)u[x].size();
REP(i, 0, sz)if(vis[u[x][i]]){
w[f[x][i]] = anc[x] + anc[u[x][i]] - 2 * anc[Find(u[x][i])];
}
}
int main(){
scanf("%d%d", &n, &m);
int a, b, c;
char st[15];
REP(i, 0, m){
scanf("%d%d%d%s", &a, &b, &c, st);
G[a].pb(b), dist[a].pb(c);
G[b].pb(a), dist[b].pb(c);
}
int k; scanf("%d", &k);
REP(i, 0, k){
scanf("%d%d", &a, &b);
u[a].pb(b), f[a].pb(i);
u[b].pb(a), f[b].pb(i);
}
Tarjan(1, 0);
REP(i, 0, k){
printf("%d\n", w[i]);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator