hdu 1535 Invitation Cards
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的更多相关文章
- HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...
- HDU 1535 Invitation Cards(最短路 spfa)
题目链接: 传送门 Invitation Cards Time Limit: 5000MS Memory Limit: 32768 K Description In the age of te ...
- HDU 1535 Invitation Cards (POJ 1511)
两次SPFA. 求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换.可是这个有1000000个点.矩阵开不了. d1[]为 1~N 的最短路. 将全部边的 邻点 交换. d ...
- hdu 1535 Invitation Cards(SPFA)
Invitation Cards Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) T ...
- HDU 1535 Invitation Cards (最短路)
题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...
- hdu 1535 Invitation Cards (最短路径)
Invitation Cards Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU - 1535 Invitation Cards 前向星SPFA
Invitation Cards In the age of television, not many people attend theater performances. Antique Come ...
- hdu 1535 Invitation Cards(spfa)
Invitation Cards Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- [HDU 1535]Invitation Cards[SPFA反向思维]
题意: (欧洲人自己写的题面就是不一样啊...各种吐槽...果断还是看晕了) 有向图, 有个源叫CCS, 求从CCS到其他所有点的最短路之和, 以及从其他所有点到CCS的最短路之和. 思路: 返回的时 ...
随机推荐
- BrainFuck语言生成器
还要求生成的代码比较快和短. 当然stackexchange上面给出了实现,java的 http://codegolf.stackexchange.com/questions/5418/brainfu ...
- Cmake 脚本对项目输出路径和输出头文件的路径定义
对Lib项目的统一输出路径以下时解决方案: set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Lib)set(CMAKE_LIBRARY_O ...
- Android 编程:calledfromWrongThreadException 的原因
子线程更新UI会发生Android.view.ViewRoot$CalledFromWrongThreadException异常的解决方法 子线程更新UI 显然假如你的程序需要执行耗时的操作的话,假如 ...
- 在Fragment中实现百度地图,定位到当前位置(基于SDKv2.1.0)
使用最新版本的百度地图需要注意的几个地方: 1.libs文件夹下要有android-support-v4.jar.baidumapapi_v2_1_0.jar.locSDK_3.1.jar三个jar包 ...
- pywin32 安装错误 ImportError: DLL load failed: 不是有效的 Win32 应用程序
pywin32 安装错误 ImportError: DLL load failed: 不是有效的 Win32 应用程序. 发现是因为没有制定Pywin32的dll所致,我们在用Pywin32开发时, ...
- 【转】Android 之 下拉框(Spinner)的使用
原文网址:http://imshare.iteye.com/blog/770950 下拉列表 Spinner. Spinner的使用,可以极大提高用户的体验性.当需要用户选择的时候,可以提供一个下拉列 ...
- FreeBsdb FAMP Lamp环境
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA1IAAAHlCAIAAABwFFq0AAAgAElEQVR4nO3d23WruhYA0JTmciiGTm
- css备忘录(关于relative、absolute)
父级用:position: relative; 子级才能用:position: absolute; relative里面用margin调位置: absolute里面用top.left.right.bo ...
- sql加强练习
1.用一条SQL语句 查询出每门课都大于80分的学生姓名 name kecheng fenshu 张三 语文 81张三 数学 75李四 语文 76李四 数学 90王五 语文 81王五 数学 100王五 ...
- jQuery.extend 和 jQuery.fn.extend
1.jQuery.extend 我们先把jQuery看成了一个类,这样好理解一些.jQuery.extend(),是扩展的jQuery这个类. 假设我们把jQuery这个类看成是人类,能吃饭能喝水能跑 ...