poj3114Countries in War(缩点+DIJK)
http://poj.org/problem?id=3114
缩点+DIJK 注意缩点之后有重边啊 floyd会TLE
#include <iostream>
#include<cstring>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<stack>
#define N 510
#define M 301000
#define INF 0xfffffff
using namespace std;
struct node
{
int u,v,next,w;
}edge[M];
int t,low[N],pre[N],sccno[N],head[N],scc,dep,ww[N][N],vis[N],dis[N];
int o[N][N];
void init()
{
t = ;
memset(head,-,sizeof(head));
}
void add(int u,int v,int w)
{
edge[t].u =u;
edge[t].v = v;
edge[t].w = w;
edge[t].next = head[u];
head[u] = t++;
}
stack<int>s;
void dfs(int u)
{
low[u] = pre[u] = ++dep;
s.push(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++;
for(;;)
{
int x = s.top();s.pop();
sccno[x] = scc;
if(x==u)break;
}
}
}
void find_scc(int n)
{
scc=;dep=;
memset(low,,sizeof(low));
memset(pre,,sizeof(pre));
memset(sccno,,sizeof(sccno));
for(int i = ; i <= n ; i++)
if(!pre[i])
dfs(i);
}
int dijk(int st,int en,int n)
{
int i,j,k,mi;
memset(vis,,sizeof(vis));
for(i = ; i <= n ; i++)
dis[i] = o[st][i];
dis[st] = ;
for(i = ;i <= n ; i++)
{
mi = INF;
for(j = ; j <= n ; j++)
if(!vis[j]&&dis[j]<=mi)
mi = dis[k=j];
vis[k] = ;
for(j = ; j <= n ; j++)
if(dis[j]>o[k][j]+dis[k])
dis[j] = o[k][j]+dis[k];
}
return dis[en];
}
int main()
{
int i,j,n,m,k,a,b,c,g;
while(cin>>n>>m)
{
if(n==&&m==)
break;
memset(ww,,sizeof(ww));
init();
for(i =; i <= m ;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
find_scc(n);
for(i = ; i <= n ; i++)
for(j = ; j <= n ;j++)
o[i][j] = INF;
for(i = ; i <= scc ; i++)
o[i][i] = ;
for(i = ; i < t ; i++)
{
int u = edge[i].u,v = edge[i].v;
if(sccno[u]!=sccno[v])
{
o[sccno[u]][sccno[v]] = min(o[sccno[u]][sccno[v]],edge[i].w);//没算重的之前 WA惨了
}
}
scanf("%d",&k);
while(k--)
{
scanf("%d%d",&a,&b);
int xx = dijk(sccno[a],sccno[b],scc);
if(xx==INF)
printf("Nao e possivel entregar a carta\n");
else
cout<<xx<<endl;
}
puts("");
}
return ;
}
poj3114Countries in War(缩点+DIJK)的更多相关文章
- POJ 3114 Countries in War(强连通)(缩点)(最短路)
Countries in War Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- Hdu4005-The war(双连通缩点)
In the war, the intelligence about the enemy is very important. Now, our troop has mastered the situ ...
- Countries in War -POJ3114Tarjan缩点+SPFA
Countries in War Time Limit: 1000MS Memory Limit: 65536K Description In the year 2050, after differe ...
- POJ3114 Countries in War (强连通分量 + 缩点 + 最短路径 + 好题)
题目链接 题意是说在几个邮局之间传送一份信件,如果出发点和终止点在同一个国家传递,则时间为0,否则让你求花费最少时间,如果不能传到,则输出Nao e possivel entregar a carta ...
- Countries in War (POJ 3114) Tarjan缩点+最短路
题目大意: 在一个有向图中,每两点间通信需要一定的时间,但同一个强连通分量里传递信息不用时间,给两点u,v求他们最小的通信时间. 解题过程: 1.首先把强连通分量缩点,然后遍历每一条边来更新两个强 ...
- HDU 4005 The war 双连通分量 缩点
题意: 有一个边带权的无向图,敌人可以任意在图中加一条边,然后你可以选择删除任意一条边使得图不连通,费用为被删除的边的权值. 求敌人在最优的情况下,使图不连通的最小费用. 分析: 首先求出边双连通分量 ...
- Countries in War(强连通分量及其缩点)
http://poj.org/problem?id=3114 题意:有n个城市,m条边,由a城市到b城市的通信时间为w,若a城市与b城市连通,b城市与a城市也连通,则a,b城市之间的通信时间为0,求出 ...
- HDU 4005 The war Tarjan+dp
The war Problem Description In the war, the intelligence about the enemy is very important. Now, o ...
- hdu 4005 The war
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005 In the war, the intelligence about the enemy is ...
随机推荐
- 深入mongoDB(1)--mongod的线程模型与网络框架
最近工作需要开始研究mongoDB,我准备从其源代码角度,对于mongod和mongos服务的架构.sharding策略. replicaset策略.数据同步容灾.索引等机制做一个本质性的了解.其代码 ...
- HttpClient使用笔记
使用版本为4.5.1 常用API: 1.获取网页内容:InputStream in = response.getEntity().getContent() 2.获取状态码:response.getSt ...
- 解决CxGrid Filter 后,通过 Dataset 循环时得出的结果与 Grid显示不同步的问题.
// 方案1: 强制cxgrid 使用 dataset的 Filter GridMaster.DataController.Filter.AutoDataSetFilter := True; /// ...
- win10 安装scrapy
在win10的环境下安装scrapy,并不能直接按照官网的手册(http://doc.scrapy.org/en/1.0/intro/install.html)一次性安装成功,根据我自己的安装过程中遇 ...
- 《C和指针》 读书笔记 -- 第9章 字符串、字符和字节
1.字符串以NUL结尾,但字符串长度不包括NUl字节. 2.复制字符串 char *strcpy(char *dst,char const *src); 3.连接字符串 char *strcat(ch ...
- 移植openssl
1.官网(ftp://ftp.openssl.org/source/old/0.9.x/)下载openssl-0.9.8k.tar.gz2.交叉编译openssl CC=arm-hisiv400-li ...
- jQuery 的 json 格式的处理问题
在实际的使用中时常会发生一些ajax验证的诡异事件,如我在一个文件中使用json传送数据,结果出现了当数据发送到服务器端时(后端主要呈现的是对json数据对象的数据库查询操作),结果显示的是数据已经插 ...
- MySQL 5.1.63 单机配置多实例(简单配置)
需求: 在一台服务器上通过源码编译安装一个版本为5.1.63版本MySQL数据库: 方案:将所有配置文件与数据等均存放在/home/zhaoshuangshuang下.在同一个MySQL中运行两个实例 ...
- (转)sqlserver游标概念与实例全面解说
首先声明:该文章转自http://www.cnblogs.com/wudiwushen/archive/2010/03/30/1700925.html 的博客 引言 我们先不讲游标的什么概念,步骤 ...
- EXTJS 4.2 资料 控件之隐藏显示setVisible、只读setDisabled
隐藏: form_Step3_1_left.form.findField('CPTypeId').setVisible(false); 显示: form_Step3_1_left.form.findF ...