题意:

有编号1~P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费。

有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1。 求所有人来回的最小费用之和。

思路:

1.两次SPFA,也就是巧妙的将路线进行了翻转。

code 1:(数据较大,不能用二维数组,用的邻接表)

 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
#define MAXN 1000000
#define INF 0x3f3f3f3f
struct node
{
int now,to,w;
}edge[MAXN];//记录每条边的信息,起始点,终止点,权值
int first[MAXN],next[MAXN];
int dis[MAXN],vis[MAXN];
int n,m;
void turnup()//反转图
{
int i,k;
for(i = ; i<=m; i++)
first[i] = next[i] = -;
for(i = ; i<m; i++)
{
k= edge[i].to;
next[i] = first[k];
first[k] = i;
}
}
void SPFA2(int start)
{
int i;
for(i = ; i<=n; i++)
dis[i] = INF;
dis[start] = ;
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push(start);
while(!Q.empty())
{
start = Q.front();
Q.pop();
vis[start] = ;
i = first[start];
while()
{
int to = edge[i].now;
if(dis[to]>dis[start]+edge[i].w)
{
dis[to]=dis[start]+edge[i].w;
if(!vis[to])
{
vis[to] = ;
Q.push(to);
}
}
i = next[i];
if(i==-)
break;
}
}
return;
}
void SPFA1(int start)
{
int i;
for(i = ; i<=n; i++)
dis[i] = INF;
dis[start] = ;
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push(start);
while(!Q.empty())
{
start = Q.front();
Q.pop();
vis[start] = ;
i=first[start];
while()
{
int to = edge[i].to;
if(dis[to]>dis[start]+edge[i].w)
{
dis[to]=dis[start]+edge[i].w;
if(!vis[to])
{
vis[to] = ;
Q.push(to);
}
}
i = next[i];
if(i==-)
break;
}
}
return;
}
int main()
{
int t,sum,i;
scanf("%d",&t);
while(t--)
{
sum = ;
scanf("%d%d",&n,&m);
for(i=; i<m; i++)
first[i]=next[i]=-;
for(i=; i<m; i++)
{
scanf("%d%d%d",&edge[i].now,&edge[i].to,&edge[i].w);
next[i]=first[edge[i].now];
first[edge[i].now]=i;
}
SPFA1();
for(i = ; i<=n; i++)
sum+=dis[i];
turnup();
SPFA2();
for(i = ; i<=n; i++)
sum+=dis[i];
printf("%d\n",sum);
}
return ;
}

这里SPFA 可以优化,关于SPFA的优化:

SLF:Small Label First 策略。

  实现方法是,设队首元素为 i,队列中要加入节点 j,在 dj<=di 时加到队首而不是队尾,否则和普通的 SPFA 一样加到队尾。

LLL:Large Label Last 策略。

  实现方法是,设队列 Q 中的队首元素为 i,距离标号的平均值为 avg(d),每次出队时,若 di>avg(d),把 i 移到队列末尾,如此反复,直到找到一个 i 使 ,di<=avg(d)将其出队。

HDU 1535 Invitation Cards(SPFA,及其优化)的更多相关文章

  1. hdu 1535 Invitation Cards(SPFA)

    Invitation Cards Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) T ...

  2. [HDU 1535]Invitation Cards[SPFA反向思维]

    题意: (欧洲人自己写的题面就是不一样啊...各种吐槽...果断还是看晕了) 有向图, 有个源叫CCS, 求从CCS到其他所有点的最短路之和, 以及从其他所有点到CCS的最短路之和. 思路: 返回的时 ...

  3. HDU 1535 Invitation Cards(最短路 spfa)

    题目链接: 传送门 Invitation Cards Time Limit: 5000MS     Memory Limit: 32768 K Description In the age of te ...

  4. HDU - 1535 Invitation Cards 前向星SPFA

    Invitation Cards In the age of television, not many people attend theater performances. Antique Come ...

  5. hdu 1535 Invitation Cards(spfa)

    Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...

  7. HDU 1535 Invitation Cards (POJ 1511)

    两次SPFA. 求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换.可是这个有1000000个点.矩阵开不了. d1[]为 1~N 的最短路. 将全部边的 邻点 交换. d ...

  8. HDU 1535 Invitation Cards (最短路)

    题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...

  9. hdu 1535 Invitation Cards (最短路径)

    Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

随机推荐

  1. 使用ant的jar任务打jar包

    <?xml version="1.0" encoding="UTF-8"?> <project name="javaTest&quo ...

  2. X窗口系统名词解释

    前端时间Gentoo的桌面环境出了点问题,发现自己对Linux的桌面环境了解的很少,于是恶补了一下知识,以下名词解释基本上都是来自维基百科的条目和<Linux程序设计(第三版)>.一般而言 ...

  3. C#中调用Windows API时的数据类型对应关系

    原文 C#中调用Windows API时的数据类型对应关系 BOOL=System.Int32 BOOLEAN=System.Int32 BYTE=System.UInt16 CHAR=System. ...

  4. Android应用开发:CardView的使用及兼容

    引言 在Google I/O 2014上,Google公布了Android L Preview版本,此版本的UI有了非常大的改变,很炫很给力!同时,Google也给出了两个可以向下兼容的控件放到了V7 ...

  5. 基于visual Studio2013解决C语言竞赛题之0907删除记录

       题目

  6. struts2之高危远程代码执行漏洞,可造成服务器被入侵,下载最新版本进行修复

          Struts2 被发现存在新的高危远程代码执行漏洞,可造成服务器被入侵,只要是Struts2版本 低于 2.3.14.3 全部存在此漏洞.目前官方已经发布了最新的版本进行修复.请将stru ...

  7. switch的方便用法

    int ch = getch(); switch(ch) { case '0' ... '9': if (in_count) { count = count * 10 + (ch - '0'); } ...

  8. Python 30分钟入门——数据类型 &amp; 控制结构

    Python是一门脚本语言,我也久闻大名,但正真系统的接触学习是在去年(2013)年底到今年(2014)年初的时候.不得不说的是Python的官方文档相当齐全,假设你是在Windows上学习Pytho ...

  9. SharePoint 2013的100个新功能之搜索(二)

    一:名称建议 人员搜索中新的“名称建议”功能,微软引入了一种简单.直观的方式来根据名称找到用户.输入一个或多个字符,查看全部以其开头的名称,在所有的用户描述数据库都可用,在人员索引中也因此一样可用.该 ...

  10. 12.5.3 UNIVERSAL:最终的祖先类:

    <pre name="code" class="html">12.5.3 UNIVERSAL:最终的祖先类: 你可以把 UNIVERSAL 看作最终 ...