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

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

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

 #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. Kafka.net使用编程入门(二)

    1.首先创建一个Topic,命令如下: kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partit ...

  2. Java日期时间,以及相互转换

    Java日期时间,以及相互转化 package com.study.string; import java.text.ParseException; import java.text.SimpleDa ...

  3. JavaScript -基础- 函数与对象

    一.JavaScript三对象 1.分类方式一 1)ECMAScript JavaScript的ECMA规范 JS本身的对象 2)Dom 操作HTML相关 3)BOM游览器对象 游览器窗口对象,全局的 ...

  4. maven 打包zip,jsw相关

    参考链接: https://blog.csdn.net/masson32/article/details/51802732

  5. 每天CSS学习之text-align

    text-align是CSS的一个属性,其作用是设置文本的对齐方式.其值如下所示: 1.left:文本左对齐.如下所示: div{ text-align:left; } 结果: 2.right:文本右 ...

  6. vue-9-动画

    transition: <div id="demo"> <button v-on:click="show = !show"> Toggl ...

  7. linux文件查看

    查看目录 #查看文件 使用  ls  命令,加上参数 -l 表示查看详细信息,-a 表示查看包含隐藏文件在内的文件.也可使用通配符,*代表任意个字符,? 表示单个字符. $ ls ch*.doc #表 ...

  8. eclipse配置和使用memory Analyse分析内存

    1. 安装 在Eclipse help -> Eclipse Marketplace下搜索Memory:  图 1-1 搜索MAT插件 按照步骤安装完成重启即可. 2. 测试代码准备 测试代码 ...

  9. 基于UVM的verilog验证(转)

    reference:https://www.cnblogs.com/bettty/p/5285785.html Abstract 本文介绍UVM框架,并以crc7为例进行UVM的验证,最后指出常见的U ...

  10. AntPathMatcher做路径匹配

    转发自: http://www.cnblogs.com/leftthen/p/5212221.html 需要看详细的请看上面的链接 这里以我这里的一个Filter 中需要对路径做例外处理,filter ...