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. LNMP、LAMP、LANMP一键安装脚本(定期更新)[转]

    这个脚本是使用shell编写,为了快速在生产环境上部署LNMP/LAMP/LANMP(Linux.Nginx/Tengine.MySQL/MariaDB/Percona.PHP),适用于CentOS/ ...

  2. 畅通project

    原文请訪问:p=174">http://xiaoshig.sinaapp.com/?p=174 畅通project Time Limit:2000MS     Memory Limit ...

  3. C++类的继承实例

    首先由三个类分别为DateType(日期类).TimeType(时间类).DateTimeType(日期时间内).详细代码例如以下: #include <iostream> using n ...

  4. Swift 2.0 封装图片折叠效果

    文/猫爪(简书作者)原文链接:http://www.jianshu.com/p/688c491580e3著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 用Swift封装图片折叠效果 b ...

  5. 关于target is null for setProperty的问题总结

    出现了这个问题,报错是 com.opensymphony.xwork2.ognl.OgnlValueStack WARN  - Error setting expression 'costRecord ...

  6. hdu1015(Safecracker )

    Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kle ...

  7. 一张图搞懂 Javascript 中的原型链、prototype、__proto__的关系 转载加自己的总结

    1. JavaScript内置对象 所谓的内置对象 指的是:JavaScript本身就自己有的对象 可以直接拿来就用.例如Array String 等等.JavaScript一共有12内置对象    ...

  8. 2、shell命令学习

    1.第一个例子 touch test.sh vim test.sh #!/bin/bash echo "hello world" chmod 755 test.sh ./test. ...

  9. VS2015试验随手记

    1.第一次安装时,未完整安装,没有安装MFC,导致可以创建MFC工程,但是不能编译 解决办法,修改安装,加入MFC 2.学习创建windows runtime component,第一次使用,可以得到 ...

  10. Linux上安装JDK

    1.下载rpm文件并安装 rpm -ivh jdk-7u51-linux-x64.rpm 2.修改/etc/profile文件,增加以下配置 export JAVA_HOME=/usr/java/jd ...