题意:

给定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)的更多相关文章

  1. 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 / ...

  2. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

  3. POJ 1511 Invitation Cards (最短路spfa)

    Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...

  4. poj 1511 Invitation Cards(最短路中等题)

    In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...

  5. poj 1511 Invitation Cards (最短路)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 33435   Accepted: 111 ...

  6. POJ 1511 Invitation Cards ( 双向单源最短路 || 最小来回花费 )

    题意 : 给出 P 个顶点以及 Q 条有向边,求第一个点到其他各点距离之和+其他各点到第一个点的距离之和的最小值 分析 : 不难看出 min( 第一个点到其他各点距离之和+其他各点到第一个点的距离之和 ...

  7. [POJ] 1511 Invitation Cards

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 18198   Accepted: 596 ...

  8. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  9. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

随机推荐

  1. C/C++预处理

    C/C++编译系统编译程序的过程为预处理.编译.链接.预处理器是在程序源文件被编译之前根据预处理指令对程序源文件进行处理的程序.预处理器指令以#号开头标识,末尾不包含分号.预处理命令不是C/C++语言 ...

  2. LBP特征

    此篇摘取 <LBP特征原理及代码实现> <LBP特征 学习笔记> 另可参考实现: <LBP特征学习及实现> <LBP特征的实现及LBP+SVM分类> & ...

  3. UVa 12186 Another Crisis 工人的请愿书

    c表示某上司上报的最少请愿下属,k表示总下属c=0.01T*k=kT/100(0.01T*k是整数)c=[0.01T*k]+1=[kT/100]+1(0.01T*k不是整数) kT=100 c=1 k ...

  4. canvas绘图出现模糊,解决方法

    在项目开发中发现,canvas有一个问题,绘制的图会出现模糊现象. 解决方法之一:将canvas元素放大2倍,然后将整个canvas元素或者其父元素缩小两倍. <!DOCTYPE html> ...

  5. php 缩略图

    <!DOCTYPE html><!-- HTML5表单 --><form method="post" action="" enct ...

  6. 476 Number Complement 数字的补数

    给定一个正整数,输出它的补数.补数是对该数的二进制表示取反.注意:    给定的整数保证在32位带符号整数的范围内.    你可以假定二进制数不包含前导零位.示例 1:输入: 5输出: 2解释: 5的 ...

  7. 3个解析url的php函数

    通过url进行传值,是php中一个传值的重要手段.所以我们要经常对url里面所带的参数进行解析,如果我们知道了url传递参数名称,例如 /index.php?name=tank&sex=1#t ...

  8. WebForm中 页面传参的总结

    页面与后台的数据传递是实现动态页面的前提---数据交互.无论是MVC还是WebFrom 都需要详细了解各种前后台的数据传输方式,熟悉每种方式的优缺点,这样才能提高网站的性能,技术上得到锻炼. 1.Fo ...

  9. ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第六天(非原创)

    文章大纲 一.课程介绍二.今日内容简单介绍三.Httpclient介绍与实战四.项目源码与资料下载五.参考文章   一.课程介绍 一共14天课程(1)第一天:电商行业的背景.淘淘商城的介绍.搭建项目工 ...

  10. html5表单新增的元素与属性

    1.表单内元素的form属性 在html4中,表单内的从属元素必须书写在表单内部, 而在html5中,可以把他们书写在页面上任何地方, 然后为该元素指定一个form属性,属性值为该表单的id,这样就可 ...