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: ...
随机推荐
- viso图插入Word中大片空白解决办法
按住CTRL-->将鼠标指向绘图页边缘-->指针变为移动符号(双向箭头)-->按下左键修改绘图页大小.
- Vmware player 12
免费版的虚拟机Vmware,体积小.运行快速... 官方下载界面 下载地址: http://yunpan.cn/cm5smywVvqS8V 访问密码 35ac 官方下载:点击下载
- Beta版本冲刺Day6
会议讨论: 628:配置Mysql的时候遇到了问题,在修改数据库用户密码时无法修改,并且服务器好像连接不上去了,其他组员则继续他们的任务.601:将一些原来的界面进行了修改,修改成了更加美观的外形. ...
- Linux(Ubuntu)下如何安装JDK
一.下载 首先,当然是要下载了. 按照需要选择不同的版本.笔者选择的是 jdk-7u45,如图: 二. 解压 将下载下来的 .tar.gz 文件解压. 使用如下命令解压: sudo tar zxvf ...
- A query was run and no Result Maps were found for the Mapped Statement 'user.insertUser!selectKey'. It's likely that neither a Result Type nor a Result Map was specified.
使用mybatis时出现异常问题: 有如下的错误 Error querying database. Cause: org.apache.ibatis.executor.ExecutorExceptio ...
- Android-动画简介
Android中动画分为3种: ween Animation:通过对场景里的对象不断做图像变换(平移.缩放.旋转)产生动画效果,即是一种渐变动画: 也称View动画:也叫渐变动画,针对View的动画, ...
- springMVC自定义注解实现用户行为验证
最近在进行项目开发的时候需要对接口做Session验证 1.自定义一个注解@AuthCheckAnnotation @Documented @Target(ElementType.METHOD) @I ...
- 【POJ 2482】Stars in Your Window
http://poj.org/problem?id=2482 线段树扫描线 #include<cstdio> #include<cstring> #include<alg ...
- 【POJ 3294】Life Forms 不小于k个字符串中的最长子串
一下午和一晚上都在刚这道题,各种错误都集齐了so sad 我的时间啊!!! 后缀数组就先做到这里吧,是在伤不起啊QAQ 出现了各种奇怪的错误,看了标算,然后乱改自己的代码,莫名其妙的改A了,后来发现用 ...
- asp.net mvc中应用缓存依赖文件(xml)的一个小demo
最近项目中加了一个通用模块,就是根据一些特殊的tag,然后根据处理这些tag在同一个视图中加载不同的model(个人觉得此功能无任何意义,只是把不同的代码放在了同一个View中). 我的处理思路是这样 ...