poj 3114 Countries in War
http://poj.org/problem?id=3114
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define maxn 300000
using namespace std; const int inf=<<;
int head[maxn],head1[maxn],dfn[maxn],low[maxn],belong[maxn],stack1[maxn],dis[maxn],cnt[maxn];
int e,ee,bcc_clock,bcnt,top,N,E,x,y,h,k,o,d;
bool vis[maxn],visi[maxn]; struct node
{
int u,v,w,next;
}p[maxn]; struct node1
{
int u,v,w,next;
}pp[maxn]; void add(int u,int v,int w)
{
p[e].u=u;
p[e].v=v;
p[e].w=w;
p[e].next=head[u];
head[u]=e++;
} void addnode(int u,int v,int w)
{
pp[ee].u=u;pp[ee].v=v;
pp[ee].w=w;
pp[ee].next=head1[u];
head1[u]=ee++;
} void tarjan(int u)
{
vis[u]=true;
dfn[u]=low[u]=++bcc_clock;
stack1[++top]=u;
for(int i=head[u]; i!=-; i=p[i].next)
{
int v=p[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])
{
bcnt++;
int j;
do
{
j=stack1[top--];
vis[j]=false;
belong[j]=bcnt;
}while(j!=u);
}
} void deal()
{
bcc_clock=,bcnt=,top=;
memset(vis,false,sizeof(vis));
memset(belong,,sizeof(belong));
memset(dfn,,sizeof(dfn));
for(int i=; i<=N; i++)
{
if(!dfn[i])
{
tarjan(i);
}
}
} void inti()
{
memset(head,-,sizeof(head));
memset(head1,-,sizeof(head1));
e=,ee=;
}
bool ralex(int u,int v,int w)
{
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
return true;
}
return false;
} bool spfa(int src)
{
memset(visi,false,sizeof(visi));
memset(cnt,,sizeof(cnt));
queue<int>q;
for(int i=; i<=N; i++)
{
dis[i]=inf;
}
dis[src]=;
visi[src]=true;
q.push(src);
while(!q.empty())
{
int u=q.front();q.pop();
visi[u]=false;
for(int i=head1[u]; i!=-; i=pp[i].next)
{
if(ralex(u,pp[i].v,pp[i].w)&&!visi[pp[i].v])
{
if((++cnt[pp[i].v])>N) return false;
visi[pp[i].v]=true;
q.push(pp[i].v);
}
}
}
return true;
} int main()
{
while(scanf("%d%d",&N,&E)!=EOF)
{
inti();
if(N==&&E==) break;
for(int i=; i<E; i++)
{
scanf("%d%d%d",&x,&y,&h);
add(x,y,h);
}
deal();
for(int i=; i<=N; i++)
{
for(int j=head[i]; j!=-; j=p[j].next)
{
int v=p[j].v;
if(belong[i]==belong[v]) addnode(i,v,);
else if(belong[i]!=belong[v]) addnode(i,v,p[j].w);
}
}
scanf("%d",&k);
for(int i=; i<k; i++)
{
scanf("%d%d",&o,&d);
spfa(o);
if(dis[d]!=inf)
printf("%d\n",dis[d]);
else
printf("Nao e possivel entregar a carta\n");
}
printf("\n");
}
return ;
}
poj 3114 Countries in War的更多相关文章
- POJ 3114 Countries in War(强连通+最短路)
POJ 3114 Countries in War 题目链接 题意:给定一个有向图.强连通分支内传送不须要花费,其它有一定花费.每次询问两点的最小花费 思路:强连通缩点后求最短路就可以 代码: #in ...
- POJ 3114 Countries in War(强连通)(缩点)(最短路)
Countries in War Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- POJ 3114 Countries in War(强联通分量+Tarjan)
题目链接 题意 : 给你两个城市让你求最短距离,如果两个城市位于同一强连通分量中那距离为0. 思路 :强连通分量缩点之后,求最短路.以前写过,总感觉记忆不深,这次自己敲完再写了一遍. #include ...
- Countries in War -POJ3114Tarjan缩点+SPFA
Countries in War Time Limit: 1000MS Memory Limit: 65536K Description In the year 2050, after differe ...
- Countries in War (POJ 3114) Tarjan缩点+最短路
题目大意: 在一个有向图中,每两点间通信需要一定的时间,但同一个强连通分量里传递信息不用时间,给两点u,v求他们最小的通信时间. 解题过程: 1.首先把强连通分量缩点,然后遍历每一条边来更新两个强 ...
- POJ Countries in War 3114
题目大意: 给定一些城市,然后再给一些寄信的路信,A,B,H代表把信从A城市寄到B城市需要H小时. 如果没有直接可以寄达的,可以先通过另外一个城市到达,比如A,B可以寄信,B,C可以寄信,那么,A,C ...
- poj 3114(强连通缩点+SPFA)
题目链接:http://poj.org/problem?id=3114 思路:题目要求很简单,就是求两点之间的花费的最短时间,不过有一个要求:如果这两个city属于同一个国家,则花费时间为0.如何判断 ...
- Countries in War(强连通分量及其缩点)
http://poj.org/problem?id=3114 题意:有n个城市,m条边,由a城市到b城市的通信时间为w,若a城市与b城市连通,b城市与a城市也连通,则a,b城市之间的通信时间为0,求出 ...
- 随机算法 poj 2576 Tug of War
Tug of War Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8187 Accepted: 2204 Descri ...
随机推荐
- 2016最新cocoapods版本更新,以及多个版本的问题
先删除之前的pod 在终端中执行下面命令. 删除目录下的podrm -rf /usr/local/bin/pod 执行gem list | grep cocoapods 查看所有与cocoapods相 ...
- A_star poj2449 k短路
赛后填坑系列QAQ 贴代码呀 #include<iostream> #include<algorithm> #include<cstdio> #include< ...
- 谁动了我的timer?——C#的垃圾回收和调试
先来看如下的一段代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 using System; using System.Threading; publi ...
- motan源码分析九:开关
在前面的文章中,我们已经发现了开关的踪影,例如cluster,motan支持多个cluster,当前的cluster因为开关关闭的情况下,就会使用下一个cluster. 1.开关相关的类和接口主要都在 ...
- ViewPager切换大量Fragment不刷新的问题
PagerAdapter,需要重写instantiateItem()加载视图,onDestroy()销毁视图FragmentPagerAdapter,每一个生成的Fargment都保存在内存中,也就是 ...
- Qt 学习之路 :进程间通信
上一章我们了解了有关进程的基本知识.我们将进程理解为相互独立的正在运行的程序.由于二者是相互独立的,就存在交互的可能性,也就是我们所说的进程间通信(Inter-Process Communicatio ...
- 转 - CSS深入理解vertical-align和line-height的基友关系
一.想死你们了 几个星期没有写文章了,好忙好痒:个把月没有写长篇了,好忙好想:半个季度没在文章中唠嗑了,好痒好想. 后面一栋楼有对夫妻在吵架,声音雄浑有力,交锋酣畅淋漓,还以为只有小乡镇才有这架势,哦 ...
- C#HttpWebResponse请求常见的状态码
成员名称 说明 Continue 等效于 HTTP 状态 100.Continue 指示客户端可能继续其请求. SwitchingProtocols 等效于 HTTP 状态 101.Switching ...
- cmd连接mysql连接:mysql-h主机地址-u用户名-p用户密码(注:u与root可以不用加)
MySQL导入导出命令1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc >wc ...
- Ci 简单分页,保证能实现
某晚,自己写项目的时候去找资料,关于CI分页的, 发现百度出来的前几名的基本都是写的都是垃圾, 要么是实现不了,要么就是坑逼 所以我自己在这里写一个,不是很完美,只是说是简单的实现了原理 有了最基本的 ...