zoukankan      html  css  js  c++  java
  • hdu 2874 Connections between cities

    Problem Description
    After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some materials needed can only be produced in certain places. So we need to transport these materials from city to city. For most of roads had been totally destroyed during the war, there might be no path between two cities, no circle exists as well.
    Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
     
    
    
    Input
    Input consists of multiple problem instances.For each instance, first line contains three integers n, m and c, 2<=n<=10000, 0<=m<10000, 1<=c<=1000000. n represents the number of cities numbered from 1 to n. Following m lines, each line has three integers i, j and k, represent a road between city i and city j, with length k. Last c lines, two integers i, j each line, indicates a query of city i and city j.
     
    
    
    Output
    For each problem instance, one line for each query. If no path between two cities, output “Not connected”, otherwise output the length of the shortest path between them.
    //题目说了没有环、、那么就可以LCA了

    #include <iostream> #include <math.h> #include <vector> #include <queue> #include <stack> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int C=2000010; const int M=20010; const int N=10010; struct Edge{ int from; int to; int v; int next; }E[M],Q[C]; int n,m; bool visit[N]; int dis[N],pre[N]; int e[N],q[N]; int Find(int x) { if(x!=pre[x]) pre[x]=Find(pre[x]); return pre[x]; } void dfs(int u) { visit[u]=true; int k,v; for(k=e[u];k!=-1;k=E[k].next) { v=E[k].to; if(!visit[v]) { dis[v]=dis[u]+E[k].v; dfs(v); pre[v]=u; } } int tp; for(k=q[u];k!=-1;k=Q[k].next) { v=Q[k].to; if(visit[v]) { tp=Find(v); Q[k].v=dis[v]-dis[tp]+dis[u]-dis[tp]; Q[k^1].v=Q[k].v; } } } int main() { int qu; int ne,nq; while(scanf("%d %d %d",&n,&m,&qu)==3) { int i,j,k; ne=0; for(i=1;i<=n;i++) { pre[i]=i;dis[i]=0; e[i]=q[i]=-1; visit[i]=false; } while(m--) { scanf("%d %d %d",&i,&j,&k); E[ne].next=e[i]; E[ne].to=j; E[ne].v=k; e[i]=ne; ++ne; E[ne].next=e[j]; E[ne].to=i; E[ne].v=k; e[j]=ne; ++ne; } nq=0; // printf("%d\n",qu); for(int l=1;l<=qu;l++) { scanf("%d %d",&i,&j); Q[nq].next=q[i]; Q[nq].from=i; Q[nq].to=j; Q[nq].v=-1; q[i]=nq; ++nq; Q[nq].next=q[j]; Q[nq].from=j; Q[nq].to=i; Q[nq].v=-1; q[j]=nq; ++nq; } for(i=1;i<=n;i++) if(!visit[i]) dfs(i); for(i=0;i<nq;i+=2) { j=Q[i].from;k=Q[i].to; if(Find(j)!=Find(k)) printf("Not connected\n"); else printf("%d\n",Q[i].v); } } return 0; }
  • 相关阅读:
    小白日记41:kali渗透测试之Web渗透-SQL手工注入(三)-猜测列名、表名、库名、字段内容,数据库写入
    Python实现百度贴吧自动顶贴机
    回文自动机简述
    计算几何基本知识整理
    考试题Speed
    [ZOJ 2112] [lg2617] Dynamic Rankings
    后缀数组(Suffix Arrary)
    莫比乌斯函数与莫比乌斯反演
    题解 洛谷 P3369 【【模板】普通平衡树】
    EXCRT(扩展中国剩余定理)
  • 原文地址:https://www.cnblogs.com/372465774y/p/3053410.html
Copyright © 2011-2022 走看看