http://acm.hdu.edu.cn/showproblem.php?pid=1535

这道题两遍spfa,第一遍sfpa之后,重新建图,所有的边逆向建边,再一次spfa就可以了。

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <algorithm>
#define maxn 1000001
using namespace std;
const int inf=; long long dis1[maxn];
long long dis2[maxn];
int e1,head[maxn],e2;
bool vis[maxn];
int cnt[maxn];
int P,Q,s,e,n;
long long w;
struct node
{
int u,v,next;
long long w;
}p[maxn],p1[maxn]; void add(int u,int v,long long w)
{
p[e1].u=u;
p[e1].v=v;
p[e1].w=w;
p[e1].next=head[u];
head[u]=e1++;
} bool spfa(int src,long long dis[])
{
queue<int>q;
memset(cnt,,sizeof(cnt));
memset(vis,false,sizeof(vis));
for(int i=; i<=n; i++) dis[i]=inf;
dis[src]=;
vis[src]=true;
q.push(src);
while(!q.empty())
{
int u=q.front(); q.pop();
vis[u]=false;
for(int i=head[u]; i!=-; i=p[i].next)
{
int vv=p[i].v;
long long ww=p[i].w;
if(dis[vv]>dis[u]+ww)
{
dis[vv]=dis[u]+ww;
if((++cnt[vv])>n) return false;
if(!vis[vv])
{
q.push(vv);
vis[vv]=true;
}
}
}
}
return true;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
e1=;
memset(head,-,sizeof(head));
scanf("%d%d",&P,&Q);
for(int i=; i<Q; i++)
{
cin>>p1[i].u>>p1[i].v>>p1[i].w;
add(p1[i].u,p1[i].v,p1[i].w);
}
n=P;
spfa(,dis1);
long long sum=;
e1=;
memset(head,-,sizeof(head));
for(int i=; i<Q; i++)
{
add(p1[i].v,p1[i].u,p1[i].w);
}
spfa(,dis2);
for(int i=; i<=n; i++)
{
sum+=(dis1[i]+dis2[i]);
}
cout<<sum<<endl;
}
return ;
}

hdu 1535 Invitation Cards的更多相关文章

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

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

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

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

  3. HDU 1535 Invitation Cards (POJ 1511)

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

  4. hdu 1535 Invitation Cards(SPFA)

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

  5. HDU 1535 Invitation Cards (最短路)

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

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

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

  7. HDU - 1535 Invitation Cards 前向星SPFA

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

  8. hdu 1535 Invitation Cards(spfa)

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

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

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

随机推荐

  1. ulimit -t 引起的kill血案

    http://my.oschina.net/leejun2005/blog/80975 http://www.cnblogs.com/hazir/p/systemtap_introduction.ht ...

  2. Leetcode:LRUCache四个版本实现

    题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  3. logstash 利用drop 丢弃过滤日志

    input { stdin { } } filter { grok { match => ["message","\s*%{TIMESTAMP_ISO8601}\s ...

  4. 如何查找到文件以后,带目录一起拷贝到新的目录? cp --parents source destination

    如何查找到文件以后,带目录一起拷贝到新的目录? cp --parents  source  destination

  5. shell输出加颜色

    shell输出加颜色 #cat a.sh #!/bin/sh blue=`tput setaf 4` reset=`tput sgr0` echo "${blue}[INFORMATION] ...

  6. Http(get,post)及HttpClient(get,post)的简单使用

    1. 使用 Http 的 Get 方式读取网络数据 import java.io.BufferedReader; import java.io.IOException; import java.io. ...

  7. 屏蔽掉返回键,menu键,Home键

    public class LockActivity extends Activity{ private static final int FLAG_HOMEKEY_DISPATCHED = 0x800 ...

  8. CFGYM 2013-2014 CT S01E03 D题 费用流模版题

    题意: n行, a房间的气球,b房间的气球 i行需要的气球,与a房的距离,b房的距离 求最小距离 #include <stdio.h> #include <string.h> ...

  9. 为iPhone6 设计自适应布局(一)

    译者的话:本文是自适应布局的巩固篇,所以对布局约束的添加操作步骤等没有详细的说明.如果看着吃力的话请先移步Swift自适应布局(Adaptive Layout)教程. Apple从iOS6加入了Aut ...

  10. 支持多QQ登录的软件

    支持多QQ登录,批量加好友,批量回复QQ消息,当然也能接收 下载链接:多QQ登录软件