题目大意:

给定一些城市,然后再给一些寄信的路信,A,B,H代表把信从A城市寄到B城市需要H小时。

如果没有直接可以寄达的,可以先通过另外一个城市到达,比如A,B可以寄信,B,C可以寄信,那么,A,C也可以寄信。

其中两个城市之间如果可以相互寄信的话,那么这两个城市是属于一个国家的,寄信可以通过电子邮件,所以所需的时间为0.

题目中有K个询问,输入A,B询问A到B之间寄信最少需要多少时间

题目分析:

先求出强联通然后构图求最短路。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
usingnamespace std;
#define INF 0x7ffffff
#define maxn 510
typedef longlong LL;
#define Min(a,b) (a<b?a:b)
#define MOD 1000000007
int m, n, Time, top, ans;
int Stack[maxn], dfn[maxn], low[maxn], blocks[maxn];
bool InStack[maxn];
typedef struct node
{
int e, w;
node(int e=,int w=): e(e), w(w) {}
}node;
vector<vector<node> > G;
vector<vector<node> > G2; void init()
{
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
ans = Time = top = ;
G.clear();
G.resize(n+);
G2.clear();
G2.resize(n+);
} void Spfa()
{
} void Tarjan(int u)
{
low[u] = dfn[u] = ++Time;
Stack[top ++] = u;
InStack[u] = true;
int len = G[u].size();
node v;
for(int i=; i<len; i++)
{
v = G[u][i]; if( !low[v.e] )
{
Tarjan(v.e);
low[u] = min(low[u], low[v.e]);
}
elseif(InStack[v.e])
low[u] = min(low[u], dfn[v.e]);
}
int k;
if(dfn[u] == low[u])
{
do
{
k = Stack[--top];
InStack[k] = false;
blocks[k] = ans;
}while(u != k);
ans ++;
}
}
int Spfa(int Star,int End)
{
int dist[maxn];
bool vis[maxn];
memset(vis, false, sizeof(vis));
for(int i=; i<ans; i++)
dist[i] = INF;
dist[Star] = ;
queue<node> Q;
node P, Pn;
Q.push(node( Star,) ); while( Q.size() )
{
P = Q.front();
Q.pop();
vis[P.e] = true;
int len = G2[P.e].size(); for(int i=; i<len; i++)
{
Pn = G2[P.e][i];
if(dist[Pn.e] > dist[P.e] + Pn.w)
{
dist[Pn.e] = dist[P.e] + Pn.w;
if(!vis[Pn.e])
Q.push(Pn.e);
}
}
} // for(int i=0; i<ans; i++)
// printf("----%d\n", dist[i]);return dist[End];
} void solve()
{
int k, a, b;
for(int i=; i<=n; i++)
{
if(!low[i])
Tarjan(i);
} for(int i=; i <= n; i++)
{
int len = G[i].size();
node v;
for(int j=; j<len; j++)
{
v = G[i][j];
a = blocks[i], b = blocks[v.e]; if(a != b)
{
G2[a].push_back(node(b,v.w) );
// printf("%d->%d,%d\n",a, b, v.w); } }
}
scanf("%d",&k); while(k --)
{
scanf("%d %d",&a, &b);
a = blocks[a], b = blocks[b];
int rel = Spfa(a,b);
if(rel == INF)
puts("Nao e possivel entregar a carta");
else
printf("%d\n", rel);
}
printf("\n");
} int main()
{
while(scanf("%d %d",&n, &m), m+n)
{
init();
while(m--)
{
int a, b, c;
scanf("%d %d %d",&a, &b, &c);
G[a].push_back( node(b,c) );
}
solve();
}
return0;
}

POJ Countries in War 3114的更多相关文章

  1. POJ 3114 Countries in War(强连通+最短路)

    POJ 3114 Countries in War 题目链接 题意:给定一个有向图.强连通分支内传送不须要花费,其它有一定花费.每次询问两点的最小花费 思路:强连通缩点后求最短路就可以 代码: #in ...

  2. POJ 3114 Countries in War(强连通)(缩点)(最短路)

                                    Countries in War Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  3. poj 3114 Countries in War

    http://poj.org/problem?id=3114 #include <cstdio> #include <cstring> #include <queue&g ...

  4. Countries in War -POJ3114Tarjan缩点+SPFA

    Countries in War Time Limit: 1000MS Memory Limit: 65536K Description In the year 2050, after differe ...

  5. Countries in War(强连通分量及其缩点)

    http://poj.org/problem?id=3114 题意:有n个城市,m条边,由a城市到b城市的通信时间为w,若a城市与b城市连通,b城市与a城市也连通,则a,b城市之间的通信时间为0,求出 ...

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

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

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

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

  8. poj 1085 Triangle War (状压+记忆化搜索)

    Triangle War Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2685   Accepted: 1061 Desc ...

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

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

随机推荐

  1. CentOS 6.7编译安装MySQL 5.6

    1.安装前准备 yum install make gcc gcc-c++ ncurses-devel perl bison-devel yum groupinstall "Developme ...

  2. Asp.net Mvc 第一回 安装,并使ASP.NET MVC页面运行起来

    直接上图吧: 1.到官方网站下载:http://www.asp.net/mvc/ Codeplex开源站点:http://www.codeplex.com/aspnet(下载源代码及Futures包) ...

  3. IE兼容问题

    1.IE下event事件没有target属性,只有srcElement属性,解决方法:使用srcObj = event.srcElement ? event.srcElement : event.ta ...

  4. java移位运算的用途

    参考下面这篇文章 http://blog.csdn.net/gaowen_han/article/details/7163104 http://jinguo.iteye.com/blog/540150 ...

  5. CSS3 颜色值HSL表示方式&简单实例

    HSL色彩模式:就是色调(Hue).饱和度(Saturation).亮度(Lightness)三个颜色通道的改变以及它们相互之间的叠加来获得各种颜色,色调(Hue)色调最大值360,饱和度和亮度有百分 ...

  6. 解决kernel headers报错

    Make sure you have updated version $ sudo apt-get update Search for kernel version (optional) $ apt- ...

  7. Deep Learning学习随记(二)Vectorized、PCA和Whitening

    接着上次的记,前面看了稀疏自编码.按照讲义,接下来是Vectorized, 翻译成向量化?暂且这么认为吧. Vectorized: 这节是老师教我们编程技巧了,这个向量化的意思说白了就是利用已经被优化 ...

  8. php输出echo、print、print_r、printf、sprintf、var_dump的区别比较

    本篇文章是对php输出echo.print.print_r.printf.sprintf.var_dump的区别进行了详细的分析介绍,需要的朋友参考下     用.net开发已经5年了,最近突然想接触 ...

  9. Android界面布局学习总结

    参考文章: http://blog.csdn.net/shakespeare001/article/details/7843460 http://www.cnblogs.com/w-y-f/p/412 ...

  10. Python面向对象OOP

    一 OOP     与C++和Java一样,Python同样具有OOP设计. 过程式:从前到后,一条一条,机器能接受的顺序性方式:方式大概为"首先你应该做什么,第二应该做什么,高级点的做点假 ...