标签:style blog http color java os io for
7 6 1 6 13 E 6 3 9 E 3 5 7 S 4 1 3 N 2 4 20 W 4 7 2 S 3 1 6 1 4 2 6
13 3 36
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long 10 #define INF 0x3f3f3f 11 using namespace std; 12 const int maxn = 100010; 13 struct arc{ 14 int to,w; 15 }; 16 struct query{ 17 int to,id; 18 }; 19 int n,m,k,ans[maxn],d[maxn],uf[maxn]; 20 bool vis[maxn]; 21 vector<arc>g[maxn]; 22 vector<query>q[maxn]; 23 int Find(int x){ 24 if(x != uf[x]) 25 uf[x] = Find(uf[x]); 26 return uf[x]; 27 } 28 void tarjan(int u,int ds){ 29 vis[u] = true; 30 d[u] = ds; 31 uf[u] = u; 32 int i; 33 for(i = 0; i < g[u].size(); i++){ 34 if(!vis[g[u][i].to]) {tarjan(g[u][i].to,ds+g[u][i].w);uf[g[u][i].to] = u;} 35 } 36 for(i = 0; i < q[u].size(); i++) 37 if(vis[q[u][i].to]){ 38 ans[q[u][i].id] = d[u]+d[q[u][i].to]-2*d[Find(q[u][i].to)]; 39 } 40 } 41 int main(){ 42 int i,j,u,v,w; 43 char ch; 44 while(~scanf("%d %d",&n,&m)){ 45 for(i = 0; i <= n; i++){ 46 g[i].clear(); 47 q[i].clear(); 48 d[i] = 0; 49 vis[i] = false; 50 } 51 for(i = 0; i < m; i++){ 52 scanf("%d %d %d %c",&u,&v,&w,&ch); 53 g[u].push_back((arc){v,w}); 54 g[v].push_back((arc){u,w}); 55 } 56 scanf("%d",&k); 57 for(i = 0; i < k; i++){ 58 scanf("%d %d",&u,&v); 59 q[u].push_back((query){v,i}); 60 q[v].push_back((query){u,i}); 61 } 62 tarjan(1,0); 63 for(i = 0; i < k; i++) 64 printf("%d\n",ans[i]); 65 } 66 return 0; 67 }
BNUOJ 2105 Distance Queries,布布扣,bubuko.com
标签:style blog http color java os io for
原文地址:http://www.cnblogs.com/crackpotisback/p/3897409.html