有一张图,若干人要从不同的点到同一个中间点,再返回,求总费用最小

中间点到各个点最小费用是普通的最短路

各个点到中间点最小费用其实就是将所有路径反向建边之后中间点到各个点的最小费用,同样用最短路就可以求出了

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int MAXM=;
struct cmp{
bool operator ()(pii a,pii b){
return a.first>b.first;
}
}; int head1[MAXM+],point1[MAXM+],val1[MAXM+],next1[MAXM+],size1;
int head2[MAXM+],point2[MAXM+],val2[MAXM+],next2[MAXM+],size2;
int n,m;
int dist1[MAXM+],dist2[MAXM+]; void add1(int a,int b,int v){
point1[size1]=b;
val1[size1]=v;
next1[size1]=head1[a];
head1[a]=size1++;
} void add2(int a,int b,int v){
point2[size2]=b;
val2[size2]=v;
next2[size2]=head2[a];
head2[a]=size2++;
} void dij(int s){
int i;
priority_queue<pii,vector<pii>,cmp>q;
memset(dist1,0x3f,sizeof(dist1));
memset(dist2,0x3f,sizeof(dist2));
dist1[s]=dist2[s]=;
q.push(make_pair(dist1[s],s));
while(!q.empty()){
pii u=q.top();
q.pop();
if(u.first>dist1[u.second])continue;
for(i=head1[u.second];~i;i=next1[i]){
int j=point1[i];
if(dist1[j]>dist1[u.second]+val1[i]){
dist1[j]=dist1[u.second]+val1[i];
q.push(make_pair(dist1[j],j));
}
}
}
while(!q.empty())q.pop();
q.push(make_pair(dist2[s],s));
while(!q.empty()){
pii u=q.top();
q.pop();
if(u.first>dist2[u.second])continue;
for(i=head2[u.second];~i;i=next2[i]){
int j=point2[i];
if(dist2[j]>dist2[u.second]+val2[i]){
dist2[j]=dist2[u.second]+val2[i];
q.push(make_pair(dist2[j],j));
}
}
}
ll ans=;
for(i=;i<=n;i++){
ans+=dist1[i];
ans+=dist2[i];
}
printf("%I64d\n",ans);
} int main(){
int t;
scanf("%d",&t);
for(int q=;q<=t;q++){
scanf("%d%d",&n,&m);
int i,a,b,v;
memset(head1,-,sizeof(head1));
size1=;
memset(head2,-,sizeof(head2));
size2=;
for(i=;i<=m;i++){
scanf("%d%d%d",&a,&b,&v);
add1(a,b,v);
add2(b,a,v);
}
dij();
} return ;
}

hdu1535 Invitation Cards 最短路的更多相关文章

  1. HDU1535——Invitation Cards(最短路径:SPAF算法+dijkstra算法)

    Invitation Cards DescriptionIn the age of television, not many people attend theater performances. A ...

  2. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  3. HDU 1535 Invitation Cards (最短路)

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

  4. hdu1535——Invitation Cards

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

  5. POJ1511 Invitation Cards —— 最短路spfa

    题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Tota ...

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

    题目链接:http://poj.org/problem?id=1511 Description In the age of television, not many people attend the ...

  7. J - Invitation Cards 最短路

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

  8. D - Silver Cow Party J - Invitation Cards 最短路

    http://poj.org/problem?id=3268 题目思路: 直接进行暴力,就是先求出举行party的地方到每一个地方的最短路,然后再求以每一个点为源点跑的最短路. 还有一种方法会快很多, ...

  9. Invitation Cards POJ - 1511 (双向单源最短路)

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

随机推荐

  1. Oracle shrink space

    一.开启表的行迁移 alter table table_name enable row movement; select 'alter table '||s.owner||'.'||s.table_n ...

  2. 阿里云服务器上安装mysql的详细步骤

    阿里云安装mysql (1)下载mysql安装包,去mysql官网下载对应的包 mysql数据库官方下载网址:   https://downloads.mysql.com/archives/commu ...

  3. OPENWRT安装配置指南之 17.01.4 LEDE

    简介 这个东西,需要看简介的就不要看下去了. 下面已刚刷进去,路由IP地址为192.168.1.1为例开始配置. 浏览器访问192.168.1.1,无密码. 一:配置上网 不管你是什么方式上网,请根据 ...

  4. C++标准异常概述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  5. 2.14 C++析构函数

    参考: http://www.weixueyuan.net/view/6345.html 总结: 析构函数就是用于回收创建对象时所消耗的各种资源. 析构函数的调用顺序与构造函数调用顺序正好是相反的. ...

  6. 2018ICPC青岛 E - Plants vs. Zombies (二分+模拟)

    ZOJ - 4062 题意:有n个植物排成一排,按顺序植物的编号是1-n,每个植物都有一个生长速率,有一个机器人,机器人可以走m步,每走一步,这个机器人就会浇一次水,浇一次水那个植物就会长 自身的生长 ...

  7. AssetBundle自动标签、打包

    using System;using System.Collections.Generic;using System.IO;using UnityEditor;using UnityEngine; / ...

  8. 基于spec互评Alpha版本

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2323] 队名:二次元梦之队 组长:刘莹莹 组员:周昊 潘世维  王玉潘 赵美增 ...

  9. 搭建Django项目

    命令行搭建Django项目 1.安装django 在指定解释器环境下安装django 1.11.9 在真实python3环境下: pip3 install django==1.11.9 在虚拟环境下: ...

  10. python map 常见用法

    python map 常见用法2017年02月01日 19:32:41 淇怪君 阅读数:548版权声明:欢迎转载,转载请注明出处 https://blog.csdn.net/Tifficial/art ...