poj3114 强连通+最短路
题意:有 n 个城市,城市之间能够通过邮件或者电子邮件传递消息,已知 m 条邮件线路,每条线路代表 A 能送邮件到 B,并且花费 V 时间,如果几个城市之间能够相互邮件送达,那么他们就在同一个国家内,他们之间就能够通过电子邮件传递消息,花费 0 时间。有 k 个询问,每次询问从点 a 到点 b 传送消息,最少需要花费多少时间。
由于多个城市能够互相送邮件,那么就在同一国家,互相传递消息不需要花费,因此首先强连通缩点,然后再对每次询问求出解就行。我一开始认为强连通缩点后是有向无环图,直接 dfs 的话对于每个询问复杂度不会很大,但是结果T了,然后换成了每个询问求一次单源最短路,然后A掉了。
#include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
typedef pair<int,int> pii; const int maxn=;
const int maxm=;
const int INF=0x3f3f3f3f; int head[][maxn],point[][maxm],nxt[][maxm],size[],val[][maxm];
int n,t,scccnt;
int stx[maxn],low[maxn],scc[maxn];
int dis[maxn];
stack<int>S; int min(int a,int b){return a<b?a:b;} void init(){
memset(head,-,sizeof(head));
size[]=size[]=;
} void add(int a,int b,int v,int c=){
point[c][size[c]]=b;
nxt[c][size[c]]=head[c][a];
val[c][size[c]]=v;
head[c][a]=size[c]++;
} struct cmp{ //将优先队列改为小根堆
bool operator()(pii a,pii b){
return a.first>b.first;
}
}; void dij(int s,int t){ //传入出发点和到达点
int i;
priority_queue<pii,vector<pii>,cmp>q;
q.push(make_pair(,s));
memset(dis,0x3f,sizeof(dis));
dis[s]=;
while(!q.empty()){
pii u=q.top();
q.pop();
if(u.first>dis[u.second])continue;
for(i=head[][u.second];~i;i=nxt[][i]){
int j=point[][i];
if(dis[j]>u.first+val[][i]){
dis[j]=u.first+val[][i];
q.push(make_pair(dis[j],j));
}
}
}
if(dis[t]==INF)printf("Nao e possivel entregar a carta\n");
else printf("%d\n",dis[t]);
} void dfs(int s){
stx[s]=low[s]=++t;
S.push(s);
for(int i=head[][s];~i;i=nxt[][i]){
int j=point[][i];
if(!stx[j]){
dfs(j);
low[s]=min(low[s],low[j]);
}
else if(!scc[j]){
low[s]=min(low[s],stx[j]);
}
}
if(low[s]==stx[s]){
scccnt++;
while(){
int u=S.top();S.pop();
scc[u]=scccnt;
if(s==u)break;
}
}
} void setscc(){
memset(stx,,sizeof(stx));
memset(scc,,sizeof(scc));
t=scccnt=;
for(int i=;i<=n;++i)if(!stx[i])dfs(i);
for(int i=;i<=n;++i){
for(int j=head[][i];~j;j=nxt[][j]){
int k=point[][j];
if(scc[i]!=scc[k]){
add(scc[i],scc[k],val[][j],);
}
}
}
} int main(){
int m;
while(scanf("%d",&n)!=EOF&&n){
scanf("%d",&m);
init();
while(m--){
int a,b,v;
scanf("%d%d%d",&a,&b,&v);
add(a,b,v);
}
setscc();
int k;
scanf("%d",&k);
while(k--){
int a,b;
scanf("%d%d",&a,&b);
if(scc[a]==scc[b])printf("0\n");
else{
dij(scc[a],scc[b]);
}
}
printf("\n");
}
return ;
}
poj3114 强连通+最短路的更多相关文章
- POJ 3114 Countries in War(强连通+最短路)
POJ 3114 Countries in War 题目链接 题意:给定一个有向图.强连通分支内传送不须要花费,其它有一定花费.每次询问两点的最小花费 思路:强连通缩点后求最短路就可以 代码: #in ...
- POJ3114强连通+spfa
题意: 给你n个点,m条有向边,q询问,每次询问给两个数a,b输出a->b的最短路,但是题目有个限制,就是在一个环上的任意两点距离为0. 思路: 简单题目,直接强连通压缩 ...
- 【转】Tarjan&LCA题集
转自:http://blog.csdn.net/shahdza/article/details/7779356 [HDU][强连通]:1269 迷宫城堡 判断是否是一个强连通★2767Proving ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 【HDOJ图论题集】【转】
=============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...
- hdu图论题目分类
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- HDU图论题单
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- POJ3114 Countries in War (强连通分量 + 缩点 + 最短路径 + 好题)
题目链接 题意是说在几个邮局之间传送一份信件,如果出发点和终止点在同一个国家传递,则时间为0,否则让你求花费最少时间,如果不能传到,则输出Nao e possivel entregar a carta ...
- NOIP2017提高组Day1T3 逛公园 洛谷P3953 Tarjan 强连通缩点 SPFA 动态规划 最短路 拓扑序
原文链接https://www.cnblogs.com/zhouzhendong/p/9258043.html 题目传送门 - 洛谷P3953 题目传送门 - Vijos P2030 题意 给定一个有 ...
随机推荐
- struts(三) ---OGNL的学习和理解
OGNL:Object graphic Navgation Language(对象图形的导航语言)
- python去除空格和换行符的方法
一.去除空格 strip() " xyz ".strip() # returns "xyz" " xyz ".lstrip() # retu ...
- 关于C++虚函数的一点点~~
Talk is cheap, show you the code: 1.(普通的) #include<cstdio> class B { public: void func() const ...
- bootstrap selectpicker
mark 一下使用 bootstrap selectpicker 遇到的一个小issue,作为下次查错使用 $('.selectpicker').selectpicker('val', 'Mustar ...
- json 后台传前台
jsonObject需实例化new.jsonObject=new JSONObject();jsonObject.put("goodslist", list);jsonObject ...
- 0010 Linux 目录操作命令
01.更改目录 cd / 返回根目录 cd ~ 返回用户根目录 cd - 返回上个操作目录目录 ,等同于cd $OLDPWD 02.查看工作目录 pwd 03.创建目录 mkdir 目录名 0 ...
- CSS权重及样式优先级问题
CSS权重值计算 一条样式规则的整体权重值包含四个独立的部分:[A, B, C, D]; (1) A 表示内联样式(写在标签的style属性中),只有 1 或者 0 两个值:对于内联样式,由于没有选择 ...
- eclipse改变theme
https://github.com/eclipse-color-theme/eclipse-color-theme.git https://github.com/eclipse-color-them ...
- hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏
huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...
- vector 的 push_back[转]
vector是用数组实现的,每次执行push_back操作,相当于底层的数组实现要重新分配大小(即先free掉原存储,后重新malloc):这种实现体现到vector实现就是每当push_back一个 ...