题意:

给定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. 力荐!35 个最好用的 Vue 开源库!

    无论是开发新手还是经验丰富的老手,我们都喜欢开源软件包.对于开发者来说,如果没有这些开源软件包,很难想象我们的生活会变得多么疲惫不堪,而且靠咖啡度日也会成为家常便饭.所幸的是,随着 Vue.js 和 ...

  2. Educational Codeforces Round 24 E

    Vova again tries to play some computer card game. The rules of deck creation in this game are simple ...

  3. DP+高精度 URAL 1036 Lucky Tickets

    题目传送门 /* 题意:转换就是求n位数字,总和为s/2的方案数 DP+高精度:状态转移方程:dp[cur^1][k+j] = dp[cur^1][k+j] + dp[cur][k]; 高精度直接拿J ...

  4. h5-18-文件操作-兼容判断

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. 转 RAC HA 高可用性

    http://www.cnblogs.com/mfrbuaa/p/4089846.html

  6. windows session 管理

    Killing an Oracle process from inside Oracle I had a following situation few days ago – I was runnin ...

  7. [在读]functional javascript

    讲javascript函数化编程的一本书,逛淘宝正好看到有一家卖英文书的,顺手就买了,目前搁置.

  8. 【转】Java集合:HashMap源码剖析

    Java集合:HashMap源码剖析   一.HashMap概述二.HashMap的数据结构三.HashMap源码分析     1.关键属性     2.构造方法     3.存储数据     4.调 ...

  9. re正则表达式讲解—初步认识

    # f = open(r"C:\Users\LENOVO\Desktop\模特.txt",'r') # 1.常规提取文档内容方法 # contacts = [] # for i i ...

  10. Java入门小知识

    软件开发什么是软件?  一系列按照特定顺序组织的计算机数据和指令的集合什么是开发?  制作软件 人机交互  软件的出现实现了人与计算机之间的更好的交互交互方式   图形化界面:这种方式简单直观,使用者 ...