POJ 3114 Countries in War

题目链接

题意:给定一个有向图。强连通分支内传送不须要花费,其它有一定花费。每次询问两点的最小花费

思路:强连通缩点后求最短路就可以

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std; const int MAXNODE = 505;
const int MAXEDGE = 255005; typedef int Type;
const Type INF = 0x3f3f3f3f; struct Edge {
int u, v;
Type dist;
Edge() {}
Edge(int u, int v, Type dist) {
this->u = u;
this->v = v;
this->dist = dist;
}
}; struct HeapNode {
Type d;
int u;
HeapNode() {}
HeapNode(Type d, int u) {
this->d = d;
this->u = u;
}
bool operator < (const HeapNode& c) const {
return d > c.d;
}
}; struct Dijkstra {
int n, m;
Edge edges[MAXEDGE];
int first[MAXNODE];
int next[MAXEDGE];
bool done[MAXNODE];
Type d[MAXNODE]; void init(int n) {
this->n = n;
memset(first, -1, sizeof(first));
m = 0;
} void add_Edge(int u, int v, Type dist) {
edges[m] = Edge(u, v, dist);
next[m] = first[u];
first[u] = m++;
} int dijkstra(int s, int t) {
priority_queue<HeapNode> Q;
for (int i = 0; i < n; i++) d[i] = INF;
d[s] = 0;
memset(done, false, sizeof(done));
Q.push(HeapNode(0, s));
while (!Q.empty()) {
HeapNode x = Q.top(); Q.pop();
int u = x.u;
if (u == t) return d[t];
if (done[u]) continue;
done[u] = true;
for (int i = first[u]; i != -1; i = next[i]) {
Edge& e = edges[i];
if (d[e.v] > d[u] + e.dist) {
d[e.v] = d[u] + e.dist;
Q.push(HeapNode(d[e.v], e.v));
}
}
}
return -1;
}
} gao; const int N = 505; int n, m;
vector<Edge> g[N]; int pre[N], dfn[N], dfs_clock, sccn, sccno[N];
stack<int> S; void dfs_scc(int u) {
pre[u] = dfn[u] = ++dfs_clock;
S.push(u);
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i].v;
if (!pre[v]) {
dfs_scc(v);
dfn[u] = min(dfn[u], dfn[v]);
} else if (!sccno[v]) dfn[u] = min(dfn[u], pre[v]);
}
if (pre[u] == dfn[u]) {
sccn++;
while (1) {
int x = S.top(); S.pop();
sccno[x] = sccn;
if (x == u) break;
}
}
} void find_scc() {
dfs_clock = sccn = 0;
memset(sccno, 0, sizeof(sccno));
memset(pre, 0, sizeof(pre));
for (int i = 1; i <= n; i++)
if (!pre[i]) dfs_scc(i);
} int main() {
while (~scanf("%d%d", &n, &m) && n) {
for (int i = 1; i <= n; i++) g[i].clear();
int u, v, w;
while (m--) {
scanf("%d%d%d", &u, &v, &w);
g[u].push_back(Edge(u, v, w));
}
find_scc();
gao.init(n);
for (int u = 1; u <= n; u++) {
for (int j = 0; j < g[u].size(); j++) {
int v = g[u][j].v;
if (sccno[u] == sccno[v]) continue;
gao.add_Edge(sccno[u] - 1, sccno[v] - 1, g[u][j].dist);
}
}
int q;
scanf("%d", &q);
while (q--) {
scanf("%d%d", &u, &v);
int tmp = gao.dijkstra(sccno[u] - 1, sccno[v] - 1);
if (tmp == -1) printf("Nao e possivel entregar a carta\n");
else printf("%d\n", tmp);
}
printf("\n");
}
return 0;
}

POJ 3114 Countries in War(强连通+最短路)的更多相关文章

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

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

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

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

  3. poj 3114 Countries in War

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

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

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

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

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

  6. Countries in War -POJ3114Tarjan缩点+SPFA

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

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

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

  8. POJ Countries in War 3114

    题目大意: 给定一些城市,然后再给一些寄信的路信,A,B,H代表把信从A城市寄到B城市需要H小时. 如果没有直接可以寄达的,可以先通过另外一个城市到达,比如A,B可以寄信,B,C可以寄信,那么,A,C ...

  9. poj 3114(强连通缩点+SPFA)

    题目链接:http://poj.org/problem?id=3114 思路:题目要求很简单,就是求两点之间的花费的最短时间,不过有一个要求:如果这两个city属于同一个国家,则花费时间为0.如何判断 ...

随机推荐

  1. Linux重装系统后SSH登录失败

    #Linux重装系统后SHH登录服务器报错 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ WARNING: REMOTE H ...

  2. linux下单独安装oracle12.1客户端

    1.安装oracle-instantclient:(默认安装即可) oracle-instantclient12.1-sqlplus-12.1.0.1.0-1.x86_64.rpmoracle-ins ...

  3. 黑马程序员——Java高级应用(一)

    ------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS ...

  4. javascript解决for循环中i取值的问题(转载)

    html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  5. Android下按钮的使用方法

    package com.hangsheng.button; import android.app.Activity; import android.os.Bundle; import android. ...

  6. C#在局域网中连接Liunx上的MySql数据库

    前期准备工作: 我所用的平台是VS2010和Ubuntu 14.04.3  LTS 一.由于MySql并没有集成在VS2010中所以要先安装MySQL Connector Net 6.9.8连接工具, ...

  7. va_list/va_start/va_arg/va_end深入分析

    http://www.cnblogs.com/justinzhang/archive/2011/09/29/2195969.html

  8. ZJOI 最小割 CQOI 不同的最小割 (最小割分治)

    题目1 ZJOI 最小割 题目大意: 求一个无向带权图两点间的最小割,询问小于等于c的点对有多少. 算法讨论: 最小割 分治 代码: #include <cstdlib> #include ...

  9. nyoj 素数环

    算法:搜索 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简便起见,我们规定每个素数环都从1开始.例如,下图就是6的一个素数环. 输 ...

  10. linq学习三个实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...