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; }
  • 相关阅读:
    动态规划----查找一个数组中存在的数学数列
    java数据结构和算法------第八章
    java数据结构和算法-----第七章
    04002_HTML表单
    雷林鹏分享:PHP 高级过滤器
    雷林鹏分享:Lua 函数
    雷林鹏分享:Lua 流程控制
    雷林鹏分享:Lua 循环
    雷林鹏分享:Lua 变量
    雷林鹏分享:Lua 数据类型
  • 原文地址:https://www.cnblogs.com/372465774y/p/3053410.html
Copyright © 2011-2022 走看看