题意:

给定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. 在nginx上部署vue项目(history模式)--demo实列;

    在很早之前,我写了一篇 关于 在nginx上部署vue项目(history模式) 但是讲的都是理论,所以今天做个demo来实战下.有必要让大家更好的理解,我发现搜索这类似的问题还是挺多的,因此在写一篇 ...

  2. Aappcloud 调到二级页面黑屏

    PartnerHead3.html 后面多了一个点

  3. Java程序操作数据库SQLserver详解

    数据库基本操作:增删改查(CRUD) crud介绍(增.删.改.查操作) CRUD是指在做计算处理时的增加(Create).查询(Retrieve)(重新得到数据).更新(Update)和删除(Del ...

  4. 如何在win7、win8、win8.1上安装使用vb6.0

    https://jingyan.baidu.com/article/915fc414fdf8fb51384b2062.html如何在win7.win8.win8.1上安装使用vb6.0 如何在win7 ...

  5. (转)新手学习System Verilog & UVM指南

    从刚接触System Verilog以及后来的VMM,OVM,UVM已经有很多年了,随着电子工业的逐步发展,国内对验证人才的需求也会急剧增加,这从各大招聘网站贴出的职位上也可以看出来,不少朋友可能想尽 ...

  6. Failure to transfer org.apache.maven.plugins:maven-compiler-plugin:jar:2.5.1

    Mac上写了一段基于Maven的java代码. 上传Git后,在windows上pull下来,eclipse里面各种错误. ArtifactTransferException:Failure to t ...

  7. JavaScript 声明全局变量与局部变量

    一.JavaScript 声明全局变量的三种方式: 声明方式一: 使用var(关键字)+变量名(标识符)的方式在function外部声明,即为全局变量,否则在function声明的是局部变量.该方式即 ...

  8. Redhat5 安装序列号及版本说明

    为了保证安装的组件和订阅相匹配,红帽企业 Linux 5 需要输入一个安装号.它被用来配置安装程序来提供正确的软件包.安装号码包含在你的订阅里. 如果您没有输入安装号码,只有核心服务器或 Deskto ...

  9. 【Android】ListView中EditText焦点问题

    一.描述: 近期一个项目中需要开发一种类似表格的界面来显示和配置参数,Android并无直接类似表格的控件支持,我采用了ListView中布局EditText和TextView来实现,其中TextVi ...

  10. Hadoop伪集群部署

    环境准备 [root@jiagoushi ~]# yum -y install lrzsz 已加载插件:fastestmirror Repository 'saltstack-repo': Error ...