POJ3114 Countries in War (强连通分量 + 缩点 + 最短路径 + 好题)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int Max = ;
const int INF = 0x3f3f3f3f;
int n, m, dfs_clock, scc_cnt, scnt;
int g[Max][Max], pre[Max], low[Max], Stack[Max], sccno[Max];
int G[Max][Max];
int head[Max], num;
struct Edge
{
int v, Next;
};
Edge edge[Max * Max];
void addEdge(int u, int v)
{
edge[num].v = v;
edge[num].Next = head[u];
head[u] = num++;
}
void init()
{
memset(head, -, sizeof(head));
memset(pre, , sizeof(pre));
//memset(low, 0, sizeof(low));
memset(sccno, , sizeof(sccno));
scnt = dfs_clock = scc_cnt = num = ;
for (int i = ; i <= n; i++)
for (int j = i; j <= n; j++)
{
if (i == j)
G[i][j] = g[i][j] = ;
else
{
g[i][j] = g[j][i] = INF;
G[i][j] = G[j][i] = INF;
}
}
}
void dfs(int u)
{
pre[u] = low[u] = ++dfs_clock;
Stack[scnt++] = u;
for (int i = head[u]; i != -; i = edge[i].Next)
{
int v = edge[i].v;
if (!pre[v])
{
dfs(v);
low[u] = min(low[u], low[v]);
}
else if (!sccno[v])
low[u] = min(low[u], pre[v]);
}
if (low[u] == pre[u])
{
scc_cnt++;
for (; ;)
{
int x = Stack[--scnt];
sccno[x] = scc_cnt;
if ( x == u)
break;
}
}
}
void find_scc()
{
for (int i = ; i <= n; i++)
{
if (!pre[i])
dfs(i);
}
}
void build_new_graphic()
{
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
if (i != j && sccno[i] != sccno[j] && g[i][j] != INF) // 不同的连通分量号建立一条有向边。
{
G[ sccno[i] ][ sccno[j] ] = min(g[i][j], G[ sccno[i] ][ sccno[j] ]);
}
}
}
}
int dist[Max], vis[Max];
void dijkstra(int start, int goal)
{
//利用起点start,终点goal来搞,以前做惯了,直接用起点是1来做了
for (int i = ; i <= scc_cnt; i++)
dist[i] = G[start][i];
memset(vis, , sizeof(vis));
dist[start] = ;
vis[start] = ;
for (int i = ; i <= scc_cnt; i++)
{
int minn = INF, pos = ; // 这里初始化pos为1,否则当下面的循环不满足条件是,执行vis[pos]会出错
for (int j = ; j <= scc_cnt; j++)
{
if (!vis[j] && minn > dist[j])
{
minn = dist[j];
pos = j;
}
}
vis[pos] = ;
for (int j = ; j <= scc_cnt; j++)
{
if (!vis[j] && dist[j] > dist[pos] + G[pos][j])
dist[j] = dist[pos] + G[pos][j];
}
}
if (dist[goal] != INF)
printf("%d\n", dist[goal]);
else
printf("Nao e possivel entregar a carta\n");
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
if (n == && m == )
break;
init();
int u, v, c;
for (int i = ; i <= m; i++)
{
scanf("%d%d%d", &u, &v, &c);
if (g[u][v] > c)
{
g[u][v] = c; // 判断重边
}
addEdge(u, v);
}
find_scc(); // 找强连通分量
//cout << scc_cnt << endl;
build_new_graphic(); // 重新构图 int k;
scanf("%d", &k);
while (k--)
{
scanf("%d%d", &u, &v);
if (sccno[u] == sccno[v]) // 同一连通分量直接输出
printf("0\n");
else
{
dijkstra(sccno[u], sccno[v]);
}
}
printf("\n");
} return ;
}
POJ3114 Countries in War (强连通分量 + 缩点 + 最短路径 + 好题)的更多相关文章
- Countries in War(强连通分量及其缩点)
http://poj.org/problem?id=3114 题意:有n个城市,m条边,由a城市到b城市的通信时间为w,若a城市与b城市连通,b城市与a城市也连通,则a,b城市之间的通信时间为0,求出 ...
- POJ1236Network of Schools[强连通分量|缩点]
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16571 Accepted: 65 ...
- POJ1236Network of Schools(强连通分量 + 缩点)
题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后 ...
- HD2767Proving Equivalences(有向图强连通分量+缩点)
题目链接 题意:有n个节点的图,现在给出了m个边,问最小加多少边是的图是强连通的 分析:首先找到强连通分量,然后把每一个强连通分量缩成一个点,然后就得到了一个DAG.接下来,设有a个节点(每个节点对应 ...
- UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)
题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...
- ZOJ3795 Grouping(强连通分量+缩点+记忆化搜索)
题目给一张有向图,要把点分组,问最少要几个组使得同组内的任意两点不连通. 首先考虑找出强连通分量缩点后形成DAG,强连通分量内的点肯定各自一组,两个强连通分量的拓扑序能确定的也得各自一组. 能在同一组 ...
- POJ2553 The Bottom of a Graph(强连通分量+缩点)
题目是问,一个有向图有多少个点v满足∀w∈V:(v→w)⇒(w→v). 把图的强连通分量缩点,那么答案显然就是所有出度为0的点. 用Tarjan找强连通分量: #include<cstdio&g ...
- uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=sh ...
- poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)
http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: ...
随机推荐
- ZeroClipboard / jquery.zclip.min.js跨浏览器复制插件使用中遇到的问题解决
之前写过一个淘宝优惠券连接PC端转手机端连接的小工具,当时写到将转换好的url复制到剪切板这块时解决了IE和火狐,就是没办法搞定Chrome,知道可以通过flash搞定,但是觉得太麻烦没有仔细研究. ...
- VC维含义
VC维含义的个人理解 有关于VC维可以在很多机器学习的理论中见到,它是一个重要的概念.在读<神经网络原理>的时候对一个实例不是很明白,通过这段时间观看斯坦福的机器学习公开课及相关补充材料, ...
- Yii2 使用小部件 Breadcrumbs
yii有两种Breadcrumbs写法,one: echo Breadcrumbs::widget([ 'itemTemplate' => "<li><i>{l ...
- Keepalived+Redis高可用部署(第二版)
更新 20150625 脚本由5个减少为4个,sh脚本指令做了精简. 修改了另外3个脚本,在日志里增加了日期显示. 新增redis数据类型,持久化,主从同步简介. 新增hiredis简介. 新增c语言 ...
- 网上找到的一个jquery版网页换肤特效
这个跟我之前在锋利的JQuery那本书里看到的那个一模一样. <!DOCTYPE html> <html> <head> <meta name="& ...
- Linux不重启的情况下添加硬盘
众所周知,SATA和SCSI是支持热插拔的,但是新装了这类支持热插拔的驱动器,系统不会马上识别的,往往我们需要重启系统来识别,但是有另外一种方法可以很方面的让系统识别新的设备.作为系统管理员,需要了解 ...
- 51nod 1076强连通
Tarjan算法来解这题.无向图可以转化为有向图来解决. #include<map> #include<queue> #include<stack> #includ ...
- highstock-处理时间需要处理世界时间偏移量
highstock的数据格式采用的是[[时间,数据],[时间,数据],[时间,数据],[时间,数据]],而时间采用的是13位的毫秒值,如[1133136000000,69.66],采用的时间格式为UT ...
- Trinity min_kmer_cov
A high min_kmer value was used to reduce noise in the assembly and to identify only transcripts that ...
- 又爱又恨系列之枚举enum
其实枚举挺简单的,只不过以前没好好学,所以不知道这个东西,恩,现在梳理一下 整体而言,首先枚举是一个数据类型,这个数据类型和结构体有点像 可以分为三个层次 1.枚举数据类型定义 第一种:enum 枚举 ...