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)的更多相关文章

  1. POJ 3114 Countries in War(强连通)(缩点)(最短路)

                                    Countries in War Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  2. Hdu4005-The war(双连通缩点)

    In the war, the intelligence about the enemy is very important. Now, our troop has mastered the situ ...

  3. Countries in War -POJ3114Tarjan缩点+SPFA

    Countries in War Time Limit: 1000MS Memory Limit: 65536K Description In the year 2050, after differe ...

  4. POJ3114 Countries in War (强连通分量 + 缩点 + 最短路径 + 好题)

    题目链接 题意是说在几个邮局之间传送一份信件,如果出发点和终止点在同一个国家传递,则时间为0,否则让你求花费最少时间,如果不能传到,则输出Nao e possivel entregar a carta ...

  5. Countries in War (POJ 3114) Tarjan缩点+最短路

    题目大意: 在一个有向图中,每两点间通信需要一定的时间,但同一个强连通分量里传递信息不用时间,给两点u,v求他们最小的通信时间.   解题过程: 1.首先把强连通分量缩点,然后遍历每一条边来更新两个强 ...

  6. HDU 4005 The war 双连通分量 缩点

    题意: 有一个边带权的无向图,敌人可以任意在图中加一条边,然后你可以选择删除任意一条边使得图不连通,费用为被删除的边的权值. 求敌人在最优的情况下,使图不连通的最小费用. 分析: 首先求出边双连通分量 ...

  7. Countries in War(强连通分量及其缩点)

    http://poj.org/problem?id=3114 题意:有n个城市,m条边,由a城市到b城市的通信时间为w,若a城市与b城市连通,b城市与a城市也连通,则a,b城市之间的通信时间为0,求出 ...

  8. HDU 4005 The war Tarjan+dp

    The war Problem Description   In the war, the intelligence about the enemy is very important. Now, o ...

  9. hdu 4005 The war

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005 In the war, the intelligence about the enemy is ...

随机推荐

  1. 声明(创建) JavaScript 变量

    在 JavaScript 中创建变量通常称为"声明"变量. 我们使用 var 关键词来声明变量: var carname; 变量声明之后,该变量是空的(它没有值). 如需向变量赋值 ...

  2. jquery nicescroll 配置参数

    jQuery滚动条插件兼容ie6+.手机.ipad http://www.areaaperta.com/nicescroll/ jQuery(function($){ $("#scrollI ...

  3. mcrypt加密与解密

    $key = 'test'; $result_array = array('name' => 'ta', 'age' => 28); $str = encode($result_array ...

  4. 两个list 求交集效率对比

    __author__ = 'daitr' #--coding:utf-8-- import datetime #方法一: #a=[2,3,4,5] #b=[2,5,8] #tmp = [val for ...

  5. Delphi摄像头操作

    /*Title:Delphi摄像头操作 *Author:Insun *Blog:http://yxmhero1989.blog.163.com *From:www.4safer.com */ 为了笔耕 ...

  6. 找不到 com.google.zxing.ResultMetadataType 异常解决

    在 https://github.com/zxing/zxing 下载二维码扫描 将 android 导入,code打成jar包运行时 报 06-14 23:43:08.690: E/AndroidR ...

  7. div中加入iframe,可以实现Ajax的功能

    div中加入iframe,可以实现Ajax的功能,如果查询的时候,比如说城市的选择,用Ajax去实现, 在.net 可以考虑用UpdatePanel,但是点击了查询刷新表格(表格需要重新刷新加载,非纯 ...

  8. kappa 一致性系数计算实例

    kappa系数在遥感分类图像的精度评估方面有重要的应用,因此学会计算kappa系数是必要的 实例1 实例2

  9. 地形图比例尺、等高距和DEM分辨率关系

    地表面的形态是很复杂的,不同地貌类型的形态是由它的相对高度、地面坡度以及所处的地势所决定的,它们是影响等高距的主要因素。从等高距计算公式可以看出,当地图比例尺和图上等高线间的最小距离简称等高线间距确定 ...

  10. 【js】IE、FF、Chrome浏览器中的JS差异介绍

    如何判断浏览器类型 转:http://www.cnblogs.com/carekee/articles/1854674.html 1.通过浏览器特有的对象 如ie 的ActiveXObject  ff ...