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. Simple Event Correlation installation and configuration

    http://searchenterpriselinux.techtarget.com/tip/Simple-Event-Correlation-installation-and-configurat ...

  2. cf B. Eight Point Sets

    http://codeforces.com/contest/334/problem/B #include <cstdio> #include <cstring> #includ ...

  3. 在Fragment中实现百度地图,定位到当前位置(基于SDKv2.1.0)

    使用最新版本的百度地图需要注意的几个地方: 1.libs文件夹下要有android-support-v4.jar.baidumapapi_v2_1_0.jar.locSDK_3.1.jar三个jar包 ...

  4. Struts2配置RESULT中TYPE的参数说明

    chain           用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息.           com.opensymphony.xwork2.Acti ...

  5. Oracle索引状态查询&索引重建

    --检查损坏索引 SELECT status, COUNT(*)   FROM dba_indexes  GROUP BY status UNION SELECT status, COUNT(*)   ...

  6. hdu1540-Tunnel Warfare (线段树区间合并)

    题意:n个村庄,有三种操作,D x 破坏位置为x的村庄,R 修复上一次被破坏的村庄,Q x 输出含有x村庄的连续村庄的最大个数.线段树搞之,区间合并. ls[maxn]为当前节点左面的连续区间,rs[ ...

  7. Raid1源代码分析--初始化流程

    初始化流程代码量比较少,也比较简单.主要是run函数.(我阅读的代码的linux内核版本是2.6.32.61) 四.初始化流程分析 run函数顾名思义,很简单这就是在RAID1开始运行时调用,进行一些 ...

  8. puppet证书重申

  9. pyqt lineedit右边显示按钮效果

    from PyQt4 import QtGui, QtCore class ButtonLineEdit(QtGui.QLineEdit): buttonClicked = QtCore.pyqtSi ...

  10. 不可视对象的自己主动实例化BUG

    PB有个隐藏BUG会占用内存.影响效率. 先来做个样例吧 (1)创建一个不可视对象n_base,勾选Autolnstantiate属性 初始化事件constructor里面写messagebox('c ...