POJ Countries in War 3114
题目大意:
给定一些城市,然后再给一些寄信的路信,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的更多相关文章
- 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
http://poj.org/problem?id=3114 #include <cstdio> #include <cstring> #include <queue&g ...
- Countries in War -POJ3114Tarjan缩点+SPFA
Countries in War Time Limit: 1000MS Memory Limit: 65536K Description In the year 2050, after differe ...
- Countries in War(强连通分量及其缩点)
http://poj.org/problem?id=3114 题意:有n个城市,m条边,由a城市到b城市的通信时间为w,若a城市与b城市连通,b城市与a城市也连通,则a,b城市之间的通信时间为0,求出 ...
- Countries in War (POJ 3114) Tarjan缩点+最短路
题目大意: 在一个有向图中,每两点间通信需要一定的时间,但同一个强连通分量里传递信息不用时间,给两点u,v求他们最小的通信时间. 解题过程: 1.首先把强连通分量缩点,然后遍历每一条边来更新两个强 ...
- POJ 3114 Countries in War(强联通分量+Tarjan)
题目链接 题意 : 给你两个城市让你求最短距离,如果两个城市位于同一强连通分量中那距离为0. 思路 :强连通分量缩点之后,求最短路.以前写过,总感觉记忆不深,这次自己敲完再写了一遍. #include ...
- poj 1085 Triangle War (状压+记忆化搜索)
Triangle War Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2685 Accepted: 1061 Desc ...
- POJ3114 Countries in War (强连通分量 + 缩点 + 最短路径 + 好题)
题目链接 题意是说在几个邮局之间传送一份信件,如果出发点和终止点在同一个国家传递,则时间为0,否则让你求花费最少时间,如果不能传到,则输出Nao e possivel entregar a carta ...
随机推荐
- 惠普 hpssacli 工具使用
查看raid卡信息(包括控制器状态.Cache状态.电池状态) # hpssacli ctrl all show status 查看raid详细信息 # hpssacli ctrl slot=0 sh ...
- CentOS 6.7编译安装MySQL 5.6
1.安装前准备 yum install make gcc gcc-c++ ncurses-devel perl bison-devel yum groupinstall "Developme ...
- Unity3D 相机跟随主角移动
这里给主相机绑定一个脚本. 脚本写为: using UnityEngine; using System.Collections; public class camerafollow : MonoBeh ...
- MSSQL批量替换网址字符串语句
1.如何批量替换ntext字段里面的数据 问题描述: 我想把数据库中News表中的字段content中的一些字符批量替换. 我的content字段是ntext类型的. 我想替换的字段是content字 ...
- 进程识别号(PID)的理解
PID(Process Identification)操作系统里指进程识别号,也就是进程标识符.操作系统里每打开一个程序都会创建一个进程ID,即PID. PID(进程控制符)英文全称为Process ...
- solve_lock-1024-大功告成
create or replace procedure solve_lock_061203(v_msg out varchar2) as v_sql varchar2(3000); --定义 v_s ...
- vim备注
① 用户path生效 在~/.bashrc中修改path,在~/.profile中source bashrc ② secureCRT着色方案 底色RGB:43 43 43 前景色RGB:221 221 ...
- AppStore上架规则
1. 条款和条件1.1 为App Store开发程序,开发者必须遵守 Program License Agreement (PLA).人机交互指南(HIG)以及开发者和苹果签订的任何协议和合同.以下规 ...
- FMDB多线程读写问题,使用FMDataBaseQueue操作可以解决同时打开一个链接de读写问题
现在ios里使用的数据库一般都是Sqlite,但是使用Sqlite有个不太好的地方就是在多线程的时候,会出现问题,sqlite只能打开一个读或者写连结.这样的话多线程就会碰到资源占用的问题. 最开始是 ...
- [LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.
class Solution { public: int singleNumber(int A[], int n) { ; ; ; i<=bits; i++) { ; ; ; j<n; j ...