POJ 1511 Invitation Cards (最短路的两种方法spfa, Dij)
题意:
给定n个点, m条路, 求1到 2 ~n的最短路之和加上2~n到1的最短路之和
分析:
裸最短路, 求其他点到源点的距离只需要把边方向再从源点求一次即可
spfa代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#define rep(i,a,b) for(int i = a; i < b; i++)
#define _rep(i,a,b) for(int i = a; i <= b; i++)
#define mem(a,n) memset(a,n,sizeof(a))
#define fre(a) freopen(a,"r", stdin);
typedef long long LL;
using namespace std;
const LL inf = 1e12 + ;
const int maxn = 1e6 + ;
inline void read(int &x)
{
int k=;
char f=;
char c=getchar();
while(c>''||c<'')
if(c=='-')
{
f=-;
c=getchar();
}
while(c<=''&&c>='')
{
k=k*+c-'';
c=getchar();
}
x = k*f;
}
struct edge
{
LL to, d;
edge(LL _to, LL _d):to(_to), d(_d) {}
};
struct
{
int to, next, d;
} node[][maxn];
int head[][maxn];
int cnt;
int n, m;
LL ans;
LL dis[maxn];
bool vis[maxn];
void dij(int st, int index)
{ fill(dis, dis+n+, inf);
mem(vis,);
dis[st] = ;
vis[st] = ;//记得入队标记第一个点
queue<int> q;
q.push();
while(!q.empty())
{
int u = q.front();
for(int i = head[index][u]; i != -; i = node[index][i].next)
{
int v = node[index][i].to;
if(!vis[v] && dis[u] + node[index][i].d < dis[v])
{
dis[v] = dis[u] + node[index][i].d;
if(!vis[v])
q.push(v);//松弛后如果没有在队中就入队
}
}
q.pop();
vis[u] = ;
}
LL sum = ; _rep(i,,n) ans += dis[i];
}
int main()
{
int T;
read(T);
while(T--)
{
cnt = ;
ans = ;
mem(head[],-);
mem(head[],-);
read(n), read(m);
rep(i,,m)
{
int u, v, d;
read(u) , read(v) , read(d) ,
node[][cnt].to = v;
node[][cnt].d = d;
node[][cnt].next = head[][u];
head[][u] = cnt; node[][cnt].to = u;
node[][cnt].d = d;
node[][cnt].next = head[][v];
head[][v] = cnt++;
}
dij(,);
dij(,);
printf("%lld\n", ans);
}
return ;
}
dijkstra代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#define rep(i,a,b) for(int i = a; i < b; i++)
#define _rep(i,a,b) for(int i = a; i <= b; i++)
#define mem(a,n) memset(a,n,sizeof(a))
#define fre(a) freopen(a,"r", stdin);
typedef long long LL;
using namespace std;
const LL inf = 1e12 + ;
const int maxn = 1e6 + ;
inline void read(int &x)
{
int k=;
char f=;
char c=getchar();
while(c>''||c<'')
if(c=='-')
{
f=-;
c=getchar();
}
while(c<=''&&c>='')
{
k=k*+c-'';
c=getchar();
}
x = k*f;
}
struct edge
{
LL to, d;
edge(LL _to, LL _d):to(_to), d(_d) {}
};
struct
{
int to, next, d;
} node[][maxn];
int head[][maxn];
int cnt;
int n, m;
LL ans;
LL dis[maxn];
bool vis[maxn];
void dij(int st, int index)
{ fill(dis, dis+n+, inf);
mem(vis,);
dis[st] = ;//dij不需要标记第一个点
priority_queue<pair<int,int>, vector< pair<int,int> >, greater<pair<int, int> > > q;//用pair的时候要记得优先队列如果不加greater<pair<int, int>> 是按从大到小排列的
q.push(make_pair(,));
while(!q.empty())
{
int u = q.top().second;
q.pop();
if(vis[u]) continue;
vis[u] = ;
for(int i = head[index][u]; i != -; i = node[index][i].next)
{
int v = node[index][i].to;
if(!vis[v] && dis[u] + node[index][i].d < dis[v])
{
dis[v] = dis[u] + node[index][i].d;
q.push(make_pair(dis[v],v));
}
}
}
LL sum = ; _rep(i,,n) ans += dis[i];
}
int main()
{
int T;
read(T);
while(T--)
{
cnt = ;
ans = ;
mem(head[],-);
mem(head[],-);
read(n), read(m);
rep(i,,m)
{
int u, v, d;
read(u) , read(v) , read(d) ,
node[][cnt].to = v;
node[][cnt].d = d;
node[][cnt].next = head[][u];
head[][u] = cnt; node[][cnt].to = u;
node[][cnt].d = d;
node[][cnt].next = head[][v];
head[][v] = cnt++;
}
dij(,);
dij(,);
printf("%lld\n", ans);
}
return ;
}
POJ 1511 Invitation Cards (最短路的两种方法spfa, Dij)的更多相关文章
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 16178 Accepted: 526 ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
- poj 1511 Invitation Cards(最短路中等题)
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- poj 1511 Invitation Cards (最短路)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 33435 Accepted: 111 ...
- POJ 1511 Invitation Cards ( 双向单源最短路 || 最小来回花费 )
题意 : 给出 P 个顶点以及 Q 条有向边,求第一个点到其他各点距离之和+其他各点到第一个点的距离之和的最小值 分析 : 不难看出 min( 第一个点到其他各点距离之和+其他各点到第一个点的距离之和 ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
随机推荐
- RobotFramework自动化测试框架(1)- RobotFramework简介
对于RobotFramework自动化测试框架,我这里会从三个单元进行阐述,希望能对你有帮助. RobotFramework简介 RobotFramework是什么? Robotframework 是 ...
- jQuery中的.html()和.text()及.val()区别
https://www.cnblogs.com/zhang-xun/p/6766264.html
- XHTML 1.0 的三种 XML 文档类型 DOCTYPE
XHTML 1.0 的三种 XML 文档类型 XHTML 1.0 规定了三种 XML 文档类型 XHTML 1.0 Strict <!DOCTYPE html PUBLIC "-//W ...
- Split方法,拆分字符串后,去除返回的空值
我们在使用Split('')方法的时候,根据指定的 '字符' 对字符串进行拆分,当'字符’为最后一个,将会拆分一个空值进行返回. 这个时候我们可以使用 string.Split(new ch ...
- elasticsearch-sql安装
Github地址:https://github.com/NLPchina/elasticsearch-sql elasticsearch-sql插件可以方便我们使用SQL语言来对elasticsear ...
- 关于ES6的Promise的使用深入理解
ES6的promise对象研究 什么叫promise? Promise对象可以理解为一次执行的异步操作,使用promise对象之后可以使用一种链式调用的方式来组织代码:让代码更加的直观. 那我们为什么 ...
- sed.exe 在bat中使用时,需要另外起一个文件
今天在windows使用sed.exe时,同一个文件死活不生效,然后换了一个bat,再来调用,就可以了,怀疑跟sed.exe的代码有关.有时间再研究
- ag-grid-vue 本地删除,不重新刷新数据
// 这是本地删除,不重新刷新数据 that.gridListOptions.api.updateRowData({ remove: [that.submitTransmitData] });
- CentOS 7下安装配置proftpd搭建ftp服务器
proftpd全称:Professional FTP daemon,是针对Wu-FTP的弱项而开发的,除了改进的安全性,还具备许多Wu-FTP没有的特点,能以Stand-alone.xinetd模式运 ...
- 如何在Ubuntu里安装Helm
Helm是什么?在战网上玩过暗黑破坏神2代的程序员们应该还记得,Helm是国度的意思. 而在计算机领域,Helm是什么? Helm是Kubernetes的一个包管理工具,有点像nodejs的npm,U ...