分析:正向加边,反向加边,然后两遍dij

#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=1e6+;
const int INF=0x3f3f3f3f;
struct Edge{
int u,v,next;
LL w;
bool operator<(const Edge &e)const{
return w>e.w;
}
}edge[N],o[N];
int head[N],tot,n,m;
LL d[N],tmp[N];
void add(int u,int v,int w){
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
priority_queue<Edge>q;
bool vis[N];
void dij(int s){
for(int i=;i<=n;++i)d[i]=-,vis[i]=;
d[s]=,q.push(Edge{,s,,});
while(!q.empty()){
while(!q.empty()&&vis[q.top().v])q.pop();
if(q.empty())break;
int u=q.top().v;
q.pop();
vis[u]=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(!vis[v]&&(d[v]==-||d[v]>d[u]+edge[i].w)){
d[v]=d[u]+edge[i].w;
q.push(Edge{,v,d[v],});
}
}
}
// return d[t];
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head)),tot=;
for(int i=;i<=m;++i){
scanf("%d%d%I64d",&o[i].u,&o[i].v,&o[i].w);
add(o[i].u,o[i].v,o[i].w);
}
dij();
for(int i=;i<=n;++i)tmp[i]=d[i];
memset(head,-,sizeof(head)),tot=;
for(int i=;i<=m;++i){
add(o[i].v,o[i].u,o[i].w);
}
dij();
LL ans=;
for(int i=;i<=n;++i)
ans+=tmp[i]+d[i];
printf("%I64d\n",ans);
}
return ;
}

POJ 1511 Invitation Cards dij的更多相关文章

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

  2. [POJ] 1511 Invitation Cards

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

  3. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

  4. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

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

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

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

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

  7. Poj 1511 Invitation Cards(spfa)

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

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

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

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

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

随机推荐

  1. python27读书笔记0.3

    #-*- coding:utf-8 -*- ##D.has_key(k): A predicate that returns True if D has a key k.##D.items(): Re ...

  2. Mac OSX 安装Python的paramiko模块经验总结

    一.简单介绍 最近需要用Python模拟登录远程服务器并自动执行一些代码,需要安装一个叫paramiko的模块. paramiko官方介绍遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接 ...

  3. Socket和SignalR

    写到一半停电了,这心情真是哔了狗了,草稿箱竟然也没有!!! 好吧,这篇文档是之前写的记录,现在来完善(还是要完善的). 导读: 附件代码实现: Socket: 定义,同步实现,异步实现,还包括了TCP ...

  4. Vijos P1062 迎春舞会之交谊舞

    题目链接:https://vijos.org/p/1062 题意:输入n(n <= 1500)个女生左边有多少个男生.每个女生都和她左边最近的男生跳舞. 输出每个女生到可以与之跳舞的男生之间有几 ...

  5. Shell面试题

    1.用Shell编程,判断一文件是不是块或字符设备文件,如果是将其拷贝到 /dev 目录下. #!/bin/bash#1.sh#判断一文件是不是字符或块设备文件,如果是将其拷贝到 /dev 目录下#f ...

  6. MVC-Html.Label(TextBox、TextArea、RadioButton、CheckBox)

    红色表示可选参数. @Html.Label("name", "value", new { @class = "class", @style ...

  7. Windows常用CMD命令

    远程桌面:mstsc 记事本:notepad        写字板:write 计算器:calc IIS重启:iisreset 60秒倒计时关机命令:tsshutdn     15秒关机:rononc ...

  8. Java使用memcached

    1.加载commons-pool-1.5.6.jar.java_memcached-release_2.6.6.jar.slf4j-api-1.6.1.jar.slf4j-simple-1.6.1.j ...

  9. android开发两种退出程序方式(killProcess,System.exit)

    KillProcess: 在android中我们如果想要程序的进程结束可以这样写: android.os.Process.killProcess(android.os.Process.myPid()) ...

  10. 解决Win8.1 / Win Server 2012 r2 下安装 Visual Studio 时一直要求重新启动的问题(原创)

    注:本文为作者原创文章,转载于引用请注明出处,谢谢. 今天在x64的英文版Windows Server 2012 r2上安装最新版的 Visual Studio 2015 Exterprise 时,提 ...