图论 --- 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 ...
随机推荐
- js操作Dom的一些方法简化
众所周知JQ的选择符很强大,一些看起来很难实现的功能只要在$符号中传入简单的字符串就可以获取到各种层级关系的DOM,而却不用考虑浏览器的兼容性.但有时候在做小项目的时候并不需要引入JQ,而又不想频繁繁 ...
- C++中new和delete的背后
关于 C++中new背后的行为, 以前已经写过一篇了 理解C++中new背后的行为, 但是里面也只是泛泛而谈,没有真凭实据, 下面我们从汇编的角度看C++编译器究竟在背后干了什么? 我们的代码很简单, ...
- Git学习笔记(9)——自定义配置
本文主要记录了Git的一些易用化的配置和别名的使用 配置Git的命令输出带有颜色,更加醒目 //配置输出颜色 $ git config --global color.ui true //取消输出颜色 ...
- C-Lodop 非典型应用
Lodop是什么? 有人说她是报表打印工具,因为那个add_print_table语句把报表统计的那点事弄了个明明白白: 有人说她是条码打印工具,因为用了她再也不用后台生成条码图片了,前端一行指令就动 ...
- 知方可补不足~SQL2008中的发布与订阅模式
回到目录 作用:完成数据库与数据库的数据同步 原理:源数据库发布需要同时的表,存储过程,或者函数:目标数据库去订阅它,当源发生变化时,目标数据库自己同步,注意,由于这个过程是SQL自动完成的,所以要求 ...
- [Java工具]Java常用在线工具集合.
转载申明: 转载自http://www.hollischuang.com/Grepcode SearchCode ProcessOn json.cn diffchecker MaHua .马克飞象 . ...
- JS工具
/*** @author Direction*/ /*** JALJA 命名空间 namespace*/var JALJA= {} ; /*** Interface Class* 接口类需要2个 ...
- 用Chrome插件对自动化测试TestWriter进行录制
1.打开Chrome浏览器,在浏览地址中输入: chrome://extensions/,并勾选开发者模式.如图: 2.点击按钮[加载已解压的扩展程序-].如图: 3.选择Testwriter客户端下 ...
- cordovas禁止横屏
cordovas禁止横屏 官网 http://cordova.apache.org/docs/en/latest/config_ref/index.html#preference 配置config.x ...
- Java-集合-第三题 有如下Student 对象, private String name; private int age; private int score; private String classNum; 其中,classNum 表示学生的班号,例如“class05”。 有如下List List list = new ArrayList(); l
第三题 有如下Student 对象, private String name; private int age; private int score; private String classNum; ...