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. sersync 实时同步工具

    出处:http://code.google.com/p/sersync/ 当前版本的sersync依赖于rsync进行同步.如下图所示,在同步主服务器上开启sersync,将监控路径中的文件同步到目标 ...

  2. Windows 8.1 (64bit) 下搭建 MongoDB 2.4.9 环境

    一.下载MongoDB 2.4.9版 进入MongoDB官方网站的下载页面. 找到Windows 64-bit版的下载链接进行下载. 二.安装MongoDB 2.4.9版 将下载的文件解压到任意目录. ...

  3. 九度OJ1172--哈夫曼树

    哈夫曼树,第一行输入一个数n,表示叶结点的个数.需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和. 输入: 输入有多组数据.每 ...

  4. javascript跨域解决方案

    最近遇到了https跨域访问http域的问题,很多朋友理所当然的认为简单,问题是https跨域访问到http域上的资源,会进行相互通信,没有解决该问题,只能把外部资源扔到了新浪的sae上,通过http ...

  5. select&pselect/poll&ppoll/epoll

    select/pselect, poll和epoll的区别 select,epoll,poll比较 select,poll,epoll进化 Handling of asynchronous event ...

  6. 在fedora 桌面上添加应用程序

    在网上下了个android studio,这个程序只是的压缩包,解压后程序也只能在SHELL下敲入studio.sh才能运行 能不能向其他程序一样,在fedora桌面上找到应用程序,点击执行呢.在网上 ...

  7. android studio如何开启与禁用版本控制vcs

    1.开启

  8. 【Nhibernate】HQL 分页

    HQL IQuery query = NHibernateHelper.OpenSession() .CreateQuery( @"from Product"); query.Se ...

  9. 【学习总结】Info.plist和pch文件的作用

      Info.plist   建立一个工程后,会在Supporting files文件夹下看到一个“Info.plist”的文件,该文件对工程做一些运行期的配置,非常重要,不能删除 项目中其他Plis ...

  10. validate[.unobtrusive]和Bootstrap实现tooltip错误提示

    validate[.unobtrusive]和Bootstrap实现tooltip错误提示 类似的文章园子里已有,请看这里,个人感觉稍显复杂,日前也打算写一个简单的给项目用,一些关键点记录于此.最终效 ...