图论 --- spfa + 链式向前星 (模板题) dlut 1218 : 奇奇与变形金刚
1218: 奇奇与变形金刚
Time Limit: 3 Sec Memory Limit: 128 MB
Submit:
130 Solved: 37
[Submit][Status][Web
Board]
Description


Input
Output
输出奇奇要走的最短路程,数据保证一定有解,每组输出占一行
Sample Input
1 3
Sample Output
Mean:
略
analyse:
两次spfa,相加即得answer。
Time complexity:O(m*e)
Source code:
//Memory Time
// 2556K 362MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAXV 10010
#define MAXE 50010
#define LL long long
using namespace std;
int T,n,m,u,v,w;
int now,home,goal;
bool vis[MAXV];
LL dis[MAXV];
namespace Adj
{
struct Node
{
int to,next,w;
bool flag;
} edge[MAXE];
int top,head[MAXV];
void init()
{
top=1;
memset(head,0,sizeof(head));
}
void addEdge(int u,int v,int w)
{
edge[top].to=v;
edge[top].w=w;
edge[top].flag=1;
edge[top].next=head[u];
head[u]=top++;
}
}
using namespace Adj;
LL spfa(int sta,int en)
{
for(int i=1;i<=n;i++)
{
vis[i]=0;
dis[i]=LLONG_MAX;
}
queue<int>Q;
Q.push(sta);
vis[sta]=1;
dis[sta]=0;
while(!Q.empty())
{
int now=Q.front();
Q.pop();
vis[now]=0;
for(int i=head[now];i;i=edge[i].next)
{
int w=edge[i].w;
int son=edge[i].to;
if(dis[now]+w<dis[son])
{
dis[son]=dis[now]+w;
if(!vis[son])
{
Q.push(son);
vis[son]=1;
}
}
}
}
return dis[en];
} void scan(int &x)
{
char c=getchar();
x=0;
while(!(c>='0'&&c<='9')) c=getchar();
while(c>='0'&&c<='9')
{
x=x*10+c-'0';
c=getchar();
}
} int main()
{
scan(T);
while(T--)
{
Adj::init();
scan(n);
scan(m);
while(m--)
{
scan(u);
scan(v);
scan(w);
addEdge(u,v,w);
}
scan(now);
scan(home);
scan(goal);
LL ans=spfa(now,home);
ans+=spfa(home,goal);
cout<<ans<<endl;
}
return 0;
}
图论 --- spfa + 链式向前星 (模板题) dlut 1218 : 奇奇与变形金刚的更多相关文章
- 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12674 Accepted: 5651 ...
- 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 19881 Accepted: 711 ...
- 【bfs+链式向前星】防御僵尸(defend)计蒜客 - 45288
题目: A 国有 n 座城市,n−1 条双向道路将这些城市连接了起来,任何两个城市都可以通过道路互通. 某日,A 国爆发了丧尸危机,所有的幸存者现在都聚集到了 A 国的首都(首都是编号为 1 的城市) ...
- Tarjan模版(链式向前星表示方法)
这道模版用到了链式向前星表示法: struct node { int v,next; }edge[]; void add(int x,int y) { edge[++cnt].next=heads[x ...
- 【数据结构】链式向前星知识点&代码
代码: struct NODE{ int to; int nxt; int c; }node[MM];//链式向前星 ; void add(int a,int b,int c){ node[lcnt] ...
- UESTC30-最短路-Floyd最短路、spfa+链式前向星建图
最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...
- UESTC 30.最短路-最短路(Floyd or Spfa(链式前向星存图))
最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...
- SPFA + 链式前向星(详解)
求最短路是图论中最基础的算法,最短路算法挺多,本文介绍SPFA算法. 关于其他最短路算法,请看我另一篇博客最短路算法详解 链式前向星概念 简单的说,就是存储图的一个数据结构.它是按照边来存图,而邻接矩 ...
- spfa+链式前向星模板
#include<bits/stdc++.h> #define inf 1<<30 using namespace std; struct Edge{ int nex,to,w ...
随机推荐
- Unable to locate secure storage module异常的解决方案
org.eclipse.equinox.security.storage.StorageException: Unable to locate secure storage module 该异常同样是 ...
- 写了一个简单的NodeJS实现的进程间通信的例子
1. cluster介绍 大家都知道nodejs是一个单进程单线程的服务器引擎,不管有多么的强大硬件,只能利用到单个CPU进行计算.所以,有人开发了第三方的cluster,让node可以利用多核CPU ...
- 基于百度翻译API开发属于自己的翻译工具
你是否每天使用着网页翻译工具?你是否遇到过这种情况,上网过程中遇到一个很长的单词但是又不能复制,要开两个浏览器,一个打开百度翻译,照着另一个网页输入单词?你安装了各种翻译软件后,又删除,只因忍受不了那 ...
- SQL SERVER--DBA 常用到的一些脚本
自己整理了一些常用到的脚本,希望对各位有用 下载地址 --================================== 妹子不能少,是吧 BTW, 妹子是我辛苦百度来的,请不要求种求介绍各种求 ...
- Lua: 好的, 坏的, 和坑爹的
好的 小巧: 20000行C代码 可以编译进182K的可执行文件 (Linux下). 可移植: 只要是有ANSI ...
- git 操作简明扼要,命令不需要多,够用就行
提升能力最快的方法就是做项目. 从前使用svn时,最开始是自己看网上教程,只会一个从服务端checkout文件,update一下,commit一下,后来使用到了分支,感觉好了不少,感觉svn还挺不错的 ...
- CSS中如何实现未知尺寸图片垂直居中
在曾经的 淘宝UED 招聘 中有这样一道题目: “使用纯CSS实现未知尺寸的图片(但高宽都小于200px)在200px的正方形容器中水平和垂直居中.” 当然出题并不是随意,而是有其现实的原因,垂直居中 ...
- Symfony2模版引擎使用说明手册
一.基本使用 {{ demo }}输出一个demo变量; {% func %}通常是包含一个twig函数例如 for; 举个for循环的例子: {% for i in 0..10 %} <em& ...
- LCA算法的理解
LCA思想: 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好的处理技巧就是在回溯到结点u的时候,u的子树已经遍历,这时候才把u结点放入合并集合中, 这样u结 ...
- SQL*Loader之CASE10
CASE10 1. SQL脚本 [oracle@node3 ulcase]$ cat ulcase10.sql rem host write sys$output "Building dem ...