缩完点后对每次询问做dijkstra即可

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#include<ctime>
#define pa pair<int,int>
#define LL long long int
using namespace std;
const int maxn=,maxm=; LL rd(){
LL x=;char c=getchar();int neg=;
while(c<''||c>''){if(c=='-') neg=-;c=getchar();}
while(c>=''&&c<='') x=x*+c-'',c=getchar();
return x*neg;
} struct Edge{
int a,b,l,ne;
}eg[maxm],eg2[maxm];
int egh[maxn],egh2[maxn],ect2,ect;
int N,M,K;
int dfn[maxn],low[maxn],bel[maxn],stk[maxn],tot,pct,sct;
int mp[maxn][maxn],dis[maxn];
bool instk[maxn]; inline void adeg(int a,int b,int l){
eg[ect].a=a;eg[ect].b=b;eg[ect].l=l;eg[ect].ne=egh[a];egh[a]=ect++;
}inline void adeg2(int a,int b,int l){
eg2[ect2].a=a;eg2[ect2].b=b;eg2[ect2].l=l;eg2[ect2].ne=egh2[a];egh2[a]=ect2++;
} void tarjan(int x){
dfn[x]=low[x]=++tot;
stk[++sct]=x;;instk[x]=;
for(int i=egh[x];i!=-;i=eg[i].ne){
int j=eg[i].b;
if(instk[j]) low[x]=min(low[x],dfn[j]);
else if(!dfn[j]){
tarjan(j);
low[x]=min(low[x],low[j]);
}
}if(low[x]==dfn[x]){
++pct;
while(sct){
instk[stk[sct]]=;
bel[stk[sct]]=pct;
if(stk[sct--]==x) break;
}
}
} int dijkstra(int s,int e){
memset(dis,,sizeof(dis));
priority_queue<pa,vector<pa>,greater<pa> > q;
dis[s]=;q.push(make_pair(,s));
while(!q.empty()){
int p=q.top().second;q.pop();
if(p==e) return dis[e];
for(int i=egh2[p];i!=-;i=eg2[i].ne){
int b=eg2[i].b;
if(dis[b]>dis[p]+eg2[i].l){
dis[b]=dis[p]+eg2[i].l;
q.push(make_pair(dis[b],b));
}
}
}return dis[e];
} int main(){
int i,j,k;
while(){
N=rd();if(!N) break;M=rd();
if(tot) printf("\n");
memset(egh,-,sizeof(egh));
memset(egh2,-,sizeof(egh2));
memset(dfn,,sizeof(dfn));
memset(instk,,sizeof(instk));
memset(mp,,sizeof(mp));
ect=ect2=tot=pct=sct=;
for(i=;i<=M;i++){
int a=rd(),b=rd(),c=rd();
adeg(a,b,c);
}for(i=;i<=N;i++) if(!dfn[i]) tarjan(i);
for(i=;i<ect;i++){
int a=bel[eg[i].a],b=bel[eg[i].b];
if(a==b) continue;
mp[a][b]=min(mp[a][b],eg[i].l);
}
for(i=;i<=pct;i++){
for(j=;j<=pct;j++){
if(mp[i][j]<=) adeg2(i,j,mp[i][j]);
}
}
for(K=rd();K;K--){
int a=rd(),b=rd();
i=dijkstra(bel[a],bel[b]);
if(i<=1e8) printf("%d\n",i);
else printf("Nao e possivel entregar a carta\n");
}
} }

poj3114 Contries in War (tarjan+dijkstra)的更多相关文章

  1. HDU 4005 The war Tarjan+dp

    The war Problem Description   In the war, the intelligence about the enemy is very important. Now, o ...

  2. luogu3953 [NOIp2017]逛公园 (tarjan+dijkstra+记忆化搜索)

    先跑一边dijkstra算出从1到i的最短距离dis[i] 然后建反向边 从n开始记忆化搜索,(p,k)表示1到p的距离=dis[p]+k的方案数 答案就是$\sum\limits_{i=0}^{k} ...

  3. POJ 3114 Tarjan+Dijkstra

    题意: 间谍在战争期间想要传递一份谍报回国,谍报可以在邮局之间传递,但这种传递是单向的,并且会少耗一些时间.但是如果两个邮局在同一个国家的话,那么谍报在这两个邮局之间传递是不消耗时间的.如果几个邮局发 ...

  4. POJ3114 Countries in War (强连通分量 + 缩点 + 最短路径 + 好题)

    题目链接 题意是说在几个邮局之间传送一份信件,如果出发点和终止点在同一个国家传递,则时间为0,否则让你求花费最少时间,如果不能传到,则输出Nao e possivel entregar a carta ...

  5. 图论_FatherChristmasFlymouse(Tarjan+dijkstra or spfa)

    堆优化Dij VS Spfa 堆优化Dij小胜一筹. 题目名字:Father Christmas flymouse (POJ 3160) 这题可以说是图论做的比较畅快的一题,比较综合,很想说一说. 首 ...

  6. 图论1 Tarjan算法

    强连通分量 模板(强联通分量个数+缩点) #include<iostream> #include<cstdio> #define MAXn 100000 #define MAX ...

  7. HDU 4005 The war (图论-tarjan)

    The war Problem Description In the war, the intelligence about the enemy is very important. Now, our ...

  8. POJ 3114 Countries in War(强联通分量+Tarjan)

    题目链接 题意 : 给你两个城市让你求最短距离,如果两个城市位于同一强连通分量中那距离为0. 思路 :强连通分量缩点之后,求最短路.以前写过,总感觉记忆不深,这次自己敲完再写了一遍. #include ...

  9. Countries in War (POJ 3114) Tarjan缩点+最短路

    题目大意: 在一个有向图中,每两点间通信需要一定的时间,但同一个强连通分量里传递信息不用时间,给两点u,v求他们最小的通信时间.   解题过程: 1.首先把强连通分量缩点,然后遍历每一条边来更新两个强 ...

随机推荐

  1. HTTP Error 500.22 - Internal Server Error 错误解决方案

    1. 首先进入IIS ,配置IIS 应用程序池的.Net Framework版本 2. 点击左侧应用程序池,再单机右侧设置,选择版本 3. 设置为经典模式 如若遇到以下错误: 解决方案:删除confi ...

  2. SQL中not in 和not exists

    在SQL中倒是经常会用到子查询,而说到子查询,一般用的是in而不是exists,先不谈效率问题,就先说说会遇到哪些问题. 用到in当取反的时候,肯定先想到的就是not in.但是在使用not in的时 ...

  3. javaScript常用API合集

    节点 1.1 节点属性 Node.nodeName   //返回节点名称,只读 Node.nodeType   //返回节点类型的常数值,只读 Node.nodeValue  //返回Text或Com ...

  4. 第七周linux内核分析

    可执行程序的装载 作者 黎静+ 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-100002900 ...

  5. WebAPI实例--第一个API

    今天终于做了第一个任务,学习API之后的第一个实例.销售设置开发API. 第一.层次结构 1.API各层 项目结构主要有五层,分别为API.BizModel.Data.DBModel.Logic. 2 ...

  6. HTML编码规则

    <!DOCTYPE html><!-- 为每一个HTML页面的第一行添加标准模式 --> <html lang="en-us"> <!-- ...

  7. 使用Eclipse可以方便的统计工程或文件的代码行数,

    使用Eclipse可以方便的统计工程或文件的代码行数,方法如下: 1.点击要统计的项目或许文件夹,在菜单栏点击Search,然后点击File... 2.选中正则表达式(Regular expressi ...

  8. 07-java学习-方法重载-idea集成开发工具学习-项目-模块-包

    方法重载的概念? 方法重载的好处? 集成开发工具idea的学习 下载 安装 设置 建项目 导入项目 建模块 导入模块 建包 复制粘贴包 建类 复制粘贴类 运行 调试

  9. Maximal GCD CodeForces - 803C (数论+思维优化)

    C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  10. shell脚本--显示文本内容

    shell脚本显示文本内容及相关的常用命令有cat.more.less.head.tail.nl 首先是cat,cat最常用的就是一次性显示文件的所有内容,如果一个文件的内容很多的话,那么就不是很方便 ...