题意:学生从A站到B站花费C元,将学生每天从‘1’号站送往其它所有站,再从所有站接回到‘1’号站,问着个过程最小花费是多少。

思路:因为数据很大所以要用SPFA,因为不仅要从1点连接各个点还要从各个点返回一点,所以需要正邻接表和逆邻接表。然后正反各跑一次SPFA,值得注意的是因为数据很大,要将INF定位0xffffffff。

#include<stdio.h>
#include<string.h>
#include<cstring>
#include<string>
#include<math.h>
#include<queue>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<cmath> #define INF 0xffffffff
#define MAX 1000005 using namespace std; int vis[MAX],a[MAX],b[MAX],k,n,m; long long dist[MAX]; struct node
{
int u,v,nextgo,nextback;
long long w;
} Map[MAX]; void Init()
{
int i,j; memset(vis,,sizeof(vis)); for(i=; i<MAX; i++)
{
a[i]=-;
b[i]=-;
dist[i]=INF*;
}
} void Add(int u,int v,int w)
{
Map[k].u=u;
Map[k].v=v;
Map[k].w=w;
Map[k].nextgo=a[u];//正向建图
Map[k].nextback=b[v];//逆向建图
a[u]=k;
b[v]=k++;
} long long gospfa()
{
long long sum=; queue<int>Q;
int start=,i;
vis[start]=;
dist[start]=;
Q.push(start); while(!Q.empty())
{
start=Q.front();
Q.pop();
vis[start]=; for(i=a[start]; i!=-; i=Map[i].nextgo)
{
int v=Map[i].v;
if(dist[v] > dist[start]+Map[i].w)
{
dist[v]=dist[start]+Map[i].w; if(!vis[v])
{
vis[v]=;
Q.push(v);
}
}
}
} for(i=; i<=n; i++)
{
sum+=dist[i];
} return sum;
} long long backspfa()
{
long long sum=; queue<int>Q;
int start=,i;
vis[start]=;
dist[start]=;
Q.push(start); while(!Q.empty())
{
start=Q.front();
Q.pop();
vis[start]=; for(i=b[start]; i!=-; i=Map[i].nextback)
{
int u=Map[i].u;
if(dist[u] > dist[start]+Map[i].w)
{
dist[u]=dist[start]+Map[i].w; if(!vis[u])
{
vis[u]=;
Q.push(u);
}
}
}
} for(i=; i<=n; i++)
{
sum+=dist[i];
} return sum;
} int main()
{
int T,i,j,u,v;
long long w,ans; scanf("%d",&T); while(T--)
{
ans=;
k=; scanf("%d%d",&n,&m); Init(); for(i=; i<=m; i++)
{
scanf("%d%d%lld",&u,&v,&w); Add(u,v,w);
} ans+=gospfa(); for(i=;i<MAX;i++)
dist[i]=INF;
memset(vis,,sizeof(vis)); ans+=backspfa(); printf("%lld\n",ans);
} return ;
}

POJ 1511 Invitation Cards 正反SPFA的更多相关文章

  1. (简单) POJ 1511 Invitation Cards,SPFA。

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

  2. POJ 1511 Invitation Cards(逆向思维 SPFA)

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

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

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

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

  5. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

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

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

  7. POJ 1511 Invitation Cards 链式前向星+spfa+反向建边

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 27200   Accepted: 902 ...

  8. SPFA算法(2) POJ 1511 Invitation Cards

    原题: Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 31230   Accepted: ...

  9. [POJ] 1511 Invitation Cards

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

随机推荐

  1. 深入理解JNI(《深入理解android》(author : 邓凡平)读书札记)

    JNI的技术特点: java能够调用native代码. native代码能够调用java代码.   JNI的技术考虑: 实现java代码的平台无关型. java语言发展初期使用C和C++代码,避免重复 ...

  2. Windows Access Token

    security descriptor A structure and associated data that contains the security information for a sec ...

  3. OpenCV——运用于pixels war游戏

    // The "Square Detector" program. // It loads several images sequentially and tries to fin ...

  4. mysql -- 备忘

    select distinct(authorid),author from forum_post where tid=1;

  5. 贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D

    http://codeforces.com/contest/724/problem/D 题目大意:给你一个串,从串中挑选字符,挑选是有条件的,按照这个条件所挑选出来的字符集合sort一定是最后选择当中 ...

  6. dfs Codeforces Round #356 (Div. 2) D

    http://codeforces.com/contest/680/problem/D 题目大意:给你一个大小为X的空间(X<=m),在该空间内,我们要尽量的放一个体积为a*a*a的立方体,且每 ...

  7. makesfx.exe (Make SFX (Self-extracting archive))

    来源: http://www.cr173.com/soft/5500.html http://74.cz/en/make-sfx/ 官方,最新 Make SFX 是一套Win32平台下能让您制作自解压 ...

  8. wl18xx编译的时候出现WARNING: "simple_open" WARNING: "wl12xx_get_platform_data"

     ................................................................................................... ...

  9. asp.net如何删除文件夹及文件内容操作

    static void DeleteDirectory(string dir) { && Directory.GetFiles(dir).Length == ) { Directory ...

  10. sql关键字过滤C#方法

    /// <summary> ///SQL注入过滤 /// </summary> /// <param name="InText">要过滤的字符串 ...