分析:正向加边,反向加边,然后两遍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. checked

    <!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...

  2. sharepoint 2013 suitbar

    参考链接:http://academy.bindtuning.com/customize-sharepoint-2013-and-office-365-suite-bar/

  3. win 8.1 网卡

    win 8.1无线网卡是好的,驱动正常.但是搜索不到附近网络

  4. AJAX安全-Session做Token

    个人思路,请大神看到了指点 个人理解token是防止扫号机或者恶意注册.恶意发表灌水,有些JS写的token算法,也会被抓出来被利用,个人感觉还是用会过期的Session做token更好,服务器存储, ...

  5. PYTHON代码摘录

    文件处理 #典型的读取文件代码 row_data = {} with open('PaceData.csv') as paces: column_heading = paces.readline(). ...

  6. VC++对话框中添加状态栏的方法

    方法一:1.添加成员变量CStatusBarCtrl m_StatusBar;2.在OnInitDialog()中加入:                  m_StatusBar.Create(WS_ ...

  7. Django数据库配置

    将Django使用数据库由默认的sqlite3更改为mysql: 1.安装mysql驱动程序 MySQLdb(mysql-python) mysqlclient Connector/Python Py ...

  8. 关于2440的裸跑程序中SD卡读后不能成功写入问题的讨论

    问题描述: TQ2440的官方裸跑程序中,对SD卡先进行读操作,然后再写,发现不能程序卡死.倘若对SD卡先写后读,程序可以正常运行,奇哉怪哉? 写数据的关键代码--> while(i < ...

  9. Python爬取17吉他网吉他谱

    最近学习吉他,一张一张保存吉他谱太麻烦,写个小程序下载吉他谱. 安装 BeautifulSoup,BeautifulSoup是一个解析HTML的库.pip install BeautifulSoup4 ...

  10. C# and JSON

    JQuery Parse JSON var obj = $.parseJSON(data); C# creates JSON string: public static class JSONHelpe ...