poj -- 3114 Countries in War(强连通+最短路)
来源:互联网 发布:mysql容量限制 编辑:程序博客网 时间:2024/06/05 16:20
题目大意:间谍在战争期间想要传递一份邮件回国,邮件可以在各个邮局之间传播,但传递是单向耗时的;如果两个邮局在同一个国家时,那么邮件在他们之间传递是不费时的;判断两个邮局在同一个国家的标准是两个邮局之间可以相互传递邮件;给定一个起点终点,问传递邮件的最短时间;
思路分析:求强连通分量缩点后,对于一个起点终点,如果在一个强连通分量中时,直接输出0,否则用Dijkstra求最短路;
代码实现:
#include<cstdio>#include<cstring>#include<vector>#include<queue>#include<algorithm>#include<iostream>#define Min(a,b) ((a)<(b)?(a):(b))#define Max(a,b) ((a)>(b)?(a):(b))#define MEM(a) (memset((a),0,sizeof(a)))#define MEME(a) (memset((a),-1,sizeof(a)))#define MEMX(a) (memset((a),0x3f,sizeof(a)))using namespace std;const int N=510;int dfn[N],low[N],st[N],belong[N],dis[N],bcnt,tim,top,s_top,top1,n,m;bool inst[N],vis[N];struct Edge{ int v1,va; Edge *next; Edge(int _v1=0,int _va=0,Edge *_next=0):v1(_v1),va(_va),next(_next){}}*head[N],*h[N],e[N*N];struct node{ int v1,va; node(int _v1=0,int _va=0):v1(_v1),va(_va){} bool operator<(const node& rhs)const { return va>rhs.va; }};vector<node> v[N];void Addedge(int from,int to,int val){ Edge *p=&e[top++]; p->v1=to; p->va=val; p->next=head[from]; head[from]=p;}void Tarjan(int i){ dfn[i]=low[i]=++tim; st[++s_top]=i; inst[i]=1; int to; for(Edge *p=head[i];p;p=p->next){ to=p->v1; if(!dfn[to]){ Tarjan(to); if(low[to]<low[i]) low[i]=low[to]; }else if(inst[to]&&dfn[to]<low[i]) low[i]=dfn[to]; } if(dfn[i]==low[i]){ bcnt++; do{ to=st[s_top--]; inst[to]=0; belong[to]=bcnt; }while(i!=to); }}int Dijkstra(int o,int d){ priority_queue<node> q; MEMX(dis),MEM(vis); dis[o]=0; q.push(node(o,0)); int head,now_v1,now_va; while(!q.empty()){ head=q.top().v1; if(head==d) return q.top().va; q.pop(); vis[head]=1; for(int i=0;i<v[head].size();++i){ now_v1=v[head][i].v1,now_va=v[head][i].va; if(!vis[now_v1]&&dis[head]+now_va<dis[now_v1]){ dis[now_v1]=dis[head]+now_va; q.push(node(now_v1,dis[now_v1])); } } } return -1;}int main(){ int x,y,hour,k,o,d; while(~scanf("%d",&n),n){ scanf("%d",&m); MEM(head),MEM(h),MEM(dfn),MEM(low),MEM(inst); for(int i=1;i<=n;++i) v[i].clear(); bcnt=top=top1=s_top=tim=0; node tmp; while(m--){ scanf("%d%d%d",&x,&y,&hour); Addedge(x,y,hour); } for(int i=1;i<=n;++i) if(!dfn[i]) Tarjan(i); for(int i=1;i<=n;++i) for(Edge *p=head[i];p;p=p->next) if(belong[i]!=belong[p->v1]){ tmp.v1=belong[p->v1],tmp.va=p->va; v[belong[i]].push_back(tmp); } scanf("%d",&k); while(k--){ scanf("%d%d",&o,&d); if(belong[o]==belong[d]){ printf("0\n"); continue; } int ans=Dijkstra(belong[o],belong[d]); if(ans!=-1) printf("%d\n",ans); else printf("Nao e possivel entregar a carta\n"); } printf("\n"); }}
0 0
- POJ 3114 Countries in War(强连通+最短路)
- poj -- 3114 Countries in War(强连通+最短路)
- poj 3114 Countries in War--强连通分量+最短路
- poj 3114 Countries in War(强连通分量+最短路)
- 【POJ】3114 Countries in War 强连通+最短路
- POJ 3114 Countries in War 强连通+最短路
- POJ 3114 Countries in War(强连通分量+最短路)
- POJ 3114 Countries in War(强连通分量 最短路)
- poj Countries in War(强连通+最短路)
- poj3114 Countries in War(强连通分量+最短路)
- poj 3114 Countries in War(强连通分量缩点+spfa求最短路)
- POJ 3114 Countries in War 强连通tarjan缩点后 跑最短路spfa
- POJ 3114 - Countries in War(强连通分量+缩点+拓扑排序+DAG最短路)
- poj 3114 Countries in War(强连通分支缩点+最短路)
- POJ 3114 Countries in War(强连通分量+Spfa)
- poj 3114 Countries in War(强连通缩点+最短路)
- poj 3114 Countries in War(缩点+最短路)
- POJ 3114 Countries in War 强连通+dijkstra .
- Android如何查看应用签名信息--微信平台开发应用的签名
- Twemproxy: Download & Install & Config & Start
- JVM研究
- windows环境下搭建spark
- Python:[转] 字符串大小写的转换
- poj -- 3114 Countries in War(强连通+最短路)
- 同构数
- XStream操作XML和JavaBean对象
- C语言“指针”终极论证,到底什么是指针
- Spring------Spring注入
- swift String extension
- php字符串函数(2)
- hdu 2003 求实数的绝对值(java)
- 从源码角度 解决Volley框架乱码的问题