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 ...
随机推荐
- IOS离线教程下载与Dash的使用
都知道,苹果官网的IOS Developer Library是开发者最喜欢用的知识仓库,但由于有时打开它实在太慢了! 但是,我们可以手动下载离线版的!离线的文档,在这里,叫做DocSet,意指文档集合 ...
- 提升PHP性能的21种方法
提升PHP性能的21种方法. 1.用单引号来包含字符串要比双引号来包含字符串更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会.2.如果能将类的方法定义成static,就尽量定义成st ...
- sql新感悟(where 1 = 1)
花了好久把YII框架看完发现一本很不错的书:SQL案例解析(清华大学出版社),看到一些比较有用的东西,感觉应该把他记录下来,看了好多页发现书中一直有 where 1=1,这样的语句,查过发现“wher ...
- 爬虫学习之基于Scrapy的网络爬虫
###概述 在上一篇文章<爬虫学习之一个简单的网络爬虫>中我们对爬虫的概念有了一个初步的认识,并且通过Python的一些第三方库很方便的提取了我们想要的内容,但是通常面对工作当作复杂的需求 ...
- (转)《深入理解java虚拟机》学习笔记5——Java Class类文件结构
Java语言从诞生之时就宣称一次编写,到处运行的跨平台特性,其实现原理是源码文件并没有直接编译成机器指令,而是编译成Java虚拟机可以识别和运行的字节码文件(Class类文件,*.class),字节码 ...
- 图像处理-07-图像的轮廓提取-Robert算子
图像的轮廓提取-Robert算子 图像的边缘:周围像素灰度有阶跃变化或“屋顶”变化的那些像素的集合,边缘广泛存在于物体与背景之间.物体与物体之间,基元与基元之间,是图像分割的重要依据. 物体的边缘是由 ...
- velocity语法
1.声明变量 #set($var = XXX) 右边可以是以下的内容 Variable reference String literal Property reference Method refer ...
- 关于mapreduce过程中出现的错误:Too many fetch-failures
Reduce task启动后第一个阶段是shuffle,即向map端fetch数据.每次fetch都可能因为connect超时,read超时,checksum错误等原因而失败.Reduce task为 ...
- myeclipse报错:Could not create the view: An unexpected exception was thrown.
打开server窗口,发现显示:Could not create the view: An unexpected exception was thrown. 此处解决方法: 关闭myeclipse 删 ...
- use sql trigger call java function
Use UDF sys_exec to do this. You can use this link to use sys_exec function. It says, sys_exec sys_e ...