题意: (欧洲人自己写的题面就是不一样啊...各种吐槽...果断还是看晕了)

有向图, 有个源叫CCS, 求从CCS到其他所有点的最短路之和, 以及从其他所有点到CCS的最短路之和.

思路:

返回的时候是多个源,但是因为终点只有一个,所以把所有边反向之后, 再SPFA一次源即可.

#include<cstdio>
#include<vector>
#include<queue>
const int MAXN=1000000+10;
typedef long long ll;
const ll inf=1e60;
using namespace std;
struct Node{
int v,w;
};
vector<Node>mp1[MAXN];//正向建图
vector<Node>mp2[MAXN];//反向建图
int n,m;
ll cost[MAXN]; void SPFA(int u,vector<Node>mp[]){
for(int i=2;i<=n;i++)cost[i]=inf;
cost[1]=0;
queue<int>Q;
Q.push(u);
while(!Q.empty()){
int u=Q.front();
Q.pop();
for(int i=0;i<mp[u].size();i++){
int v=mp[u][i].v;
int w=mp[u][i].w;
if(cost[v]>cost[u]+w){
cost[v]=cost[u]+w;
Q.push(v);
}
}
}
} int main(){
int _case;
scanf("%d",&_case);
while(_case--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
mp1[i].clear();
mp2[i].clear();
}
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
Node p1,p2;
p1.v=u,p2.v=v,p1.w=p2.w=w;
mp1[u].push_back(p2);
mp2[v].push_back(p1);
}
SPFA(1,mp1);//正向求一次
ll ans=0;
for(int i=2;i<=n;i++){
ans+=cost[i];
}
SPFA(1,mp2);//反向求一次
for(int i=2;i<=n;i++){
ans+=cost[i];
}
printf("%lld\n",ans);
}
return 0;
}

自己敲一遍:

#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int MAXN = 1e6+5;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
typedef struct node
{
int v,w;
node(){}
node(int _v, int _w):v(_v),w(_w){}
}node; int n,m;
bool inq[MAXN];
ll cost[MAXN];
vector<node> g1[MAXN],g2[MAXN]; ll SPFA(int op)
{
memset(cost,0x3f,sizeof(cost));
memset(inq,false,sizeof(inq));
cost[1] = 0;
queue<int> q;
q.push(1);
inq[1] = true;
while(!q.empty())
{
int now = q.front();q.pop();
inq[now] = false;
for(int i=0,v,w;i<((op==1)?g1[now].size():g2[now].size());i++)
{
if(op==1)
{
v = g1[now][i].v, w = g1[now][i].w;
}
else
{
v = g2[now][i].v, w = g2[now][i].w;
}
if(cost[v] > cost[now] + w)
{
cost[v] = cost[now] + w;
if(!inq[v])
{
q.push(v);
inq[v] = true;
}
}
}
}
ll ret = 0;
for(int i=2;i<=n;i++)
ret += cost[i];
return ret;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
g1[i].clear();
g2[i].clear();
}
for(int i=0,u,v,w;i<m;i++)
{
scanf("%d %d %d",&u,&v,&w);
g1[u].push_back(node(v,w));
g2[v].push_back(node(u,w));
}
ll ans = 0;
ans += SPFA(1);
ans += SPFA(2);
printf("%d\n",(int)ans);
}
}

[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,及其优化)

    题意: 有编号1-P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费. 有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1. ...

  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 (最短路径)

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

  8. HDU 1535 Invitation Cards (最短路)

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

  9. HDU 1535 Invitation Cards (POJ 1511)

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

随机推荐

  1. C++11 多线程 基础

    C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用 ...

  2. Javascript进阶篇——总结--DOM案例+选项卡效果

    断断续续的把慕课的JavaScript基础和进阶看完了,期间不怎么应用有的都忘记了,接下来多开始写些效果,进行实际应用. 制作一个表格,显示班级的学生信息. 1. 鼠标移到不同行上时背景色改为色值为 ...

  3. Controller返回值类型ActionResult

    在mvc中所有的controller类都必须使用"Controller"后缀来命名 并且对Action也有一定的要求: 必须是一个public方法 必须是实例方法 没有标志NonA ...

  4. html5新特性--音频视频,拖放

    1.音频 <audio controls> <source src="aaa.ogg" type="audio/ogg"> <so ...

  5. 将实体转成XML,XML节点顺序由我控制

    一.前言 由于有时候返回xml格式比较严格,需要按照一定的顺序排列节点才能够符合要求,这里主要用到了自定义一个List<string> 字符顺序,再让实体属性按照List定义好的顺序重新排 ...

  6. C#界面设计疑问2:panel摆放问题

    1.问题1是这样的,网友意思让使用一个按键对应显示一个panel 即,http://zhidao.baidu.com/question/1924974374730559427.html 2.那么我在设 ...

  7. MFC 多线程

    MFC对多线程编程的支持 MFC中有两类线程,分别称之为工作者线程和用户界面线程.二者的主要区别在于工作者线程没有消息循环,而用户界面线程有自己的消息   队列和消息循环. 工作者线程没有消息机制,通 ...

  8. Java 中关键字transient引出序列化与反序列化

    一:transient(临时的)关键字 1.transient关键字只能修饰变量,而不能修饰方法和类.注意,本地变量是不能被transient关键字修饰的. 2.被transient关键字修饰的变量不 ...

  9. R语言的数据结构

    首先声明,R语言对大小写敏感. 一.向量 vector类型可以存储数字.字符和逻辑类型.构建函数为C(): > a <- c(1,2,3,4) > b <- c('dau',' ...

  10. ACM ICPC Team

    Link: https://www.hackerrank.com/challenges/acm-icpc-team/submissions/code/11617807 def count_max_to ...