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) ...
随机推荐
- C++构造函数与析构函数的解析
创建一个对象时,常常需要作某些初始化的工作,例如对数据成员赋初值. 注意,类的数据成员是不能在声明类时初始化的.如果一个类中所有的成员都是公用的,则可以在定义对象时对数据成员进行初始化.如: clas ...
- [CQOI2014]通配符匹配
Description 几乎所有操作系统的命令行界面(CLI)中都支持文件名的通配符匹配以方便用户.最常见的通配符有两个,一个是星号(""'),可以匹配0个及以上的任意字符:另一个 ...
- 学习JavaScript数据结构与算法 (二)
学习JavaScript数据结构与算法 的笔记 包含第四章队列, 第五章链表 本人所有文章首发在博客园: http://www.cnblogs.com/zhangrunhao/ 04队列 实现基本队列 ...
- 为什么站点使用https加密之后还能看到相关数据
为什么站点使用了https加密之后,还是能够用firebug之类的软件查看到提交到的信息,并且还是明文的?例如说这样: 这是因为:https(ssl)加密是发生在应用层与传输层之间,所以在传输层看到的 ...
- HTML form label
在表单布局中会遇到label标签的使用,label没有任何样式效果,有触发对应表单控件功能.比如我们点击单选按钮或多选框前文字对应选项就能被选中,这个就是对文字加了<label>标签实现. ...
- 153 Find Minimum in Rotated Sorted Array 旋转数组的最小值
假设一个按照升序排列的有序数组从某未知的位置旋转.(比如 0 1 2 4 5 6 7 可能变成 4 5 6 7 0 1 2).找到其中最小的元素.你可以假设数组中不存在重复的元素.详见:https:/ ...
- KEIL软件仿真死在等待外部晶振起振
这是由于是Debug里面的设置有问题 主要是下面2项设置 Dialog DLL默认是DCM3.DLL Parameter默认是-pCM3 应改为 Dialog DLL默认是DARMSTM.DLL Pa ...
- android开发学习 ------- 【转】Genymotion 小白安装
参考 https://www.cnblogs.com/whycxb/p/6850454.html 很详细,全面
- LeetCode 69 题
1.题目要求 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出 ...
- CF962E Byteland, Berland and Disputed Cities
思路: http://codeforces.com/blog/entry/58869. 实现: #include <bits/stdc++.h> using namespace std; ...