题意:

有编号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. xp对opengl的支持问题

    我在项目中遇到的xp显示问题是因为xp对opengl的支持问题,是通过void QCoreApplication::setAttribute(Qt::ApplicationAttribute attr ...

  2. Windows Service的官方描述,抄下来(不写obj就是LocalSystem)

    How to create a Windows service by using Sc.exe Email Print Support for Windows XP has ended Micro ...

  3. Qt学习之路(60): 创建shared library

    前段时间说了Qt一些类库的使用,今天来换一下口味,来看一下程序设计的问题.今天来说的是关于共享库 shared library. 如果你打开一些 Windows 应用程序的目录,你会发现有很多程序的 ...

  4. RESTful最佳实践之基于 jersey 的增删改查

    jersey-rest-demo 增删改查 项目地址:https://github.com/CoderDream/jersey-rest-demo 源代码:http://download.csdn.n ...

  5. mysql Emoji表情字符集转换

    <pre name="code" class="html">Java代码 java.sql.SQLException: Incorrect stri ...

  6. WPF中控件ListView和DataGrid典型属性介绍

    ListView GridView View视图 重要属性: public bool AllowsColumnReorder 获取或设置一个值,该值指示 System.Windows.Controls ...

  7. HDOJ 5088 Revenge of Nim II 位运算

    位运算.. .. Revenge of Nim II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  8. 通过ip拨号器来了解广播接收者

    1.继承广播接收者类 package com.example.ipdail; import android.content.BroadcastReceiver; import android.cont ...

  9. UVa 11045 My T-shirt suits me / 二分图

    二分图建图 判断是否是完全匹配就行 最大流也行 #include <cstdio> #include <cstring> const int MAX = 300; int a[ ...

  10. OpenProcessToken令牌函数使用方法

    >GetCurrentProcessID 得到当前进程的ID OpenProcessToken得到进程的令牌句柄LookupPrivilegeValue 查询进程的权限AdjustTokenPr ...