hdu1535 Invitation Cards 最短路
有一张图,若干人要从不同的点到同一个中间点,再返回,求总费用最小
中间点到各个点最小费用是普通的最短路
各个点到中间点最小费用其实就是将所有路径反向建边之后中间点到各个点的最小费用,同样用最短路就可以求出了
- #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 最短路的更多相关文章
- HDU1535——Invitation Cards(最短路径:SPAF算法+dijkstra算法)
Invitation Cards DescriptionIn the age of television, not many people attend theater performances. A ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- HDU 1535 Invitation Cards (最短路)
题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...
- hdu1535——Invitation Cards
Invitation Cards Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- POJ1511 Invitation Cards —— 最短路spfa
题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Tota ...
- POJ-1511 Invitation Cards( 最短路,spfa )
题目链接:http://poj.org/problem?id=1511 Description In the age of television, not many people attend the ...
- J - Invitation Cards 最短路
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- D - Silver Cow Party J - Invitation Cards 最短路
http://poj.org/problem?id=3268 题目思路: 直接进行暴力,就是先求出举行party的地方到每一个地方的最短路,然后再求以每一个点为源点跑的最短路. 还有一种方法会快很多, ...
- Invitation Cards POJ - 1511 (双向单源最短路)
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
随机推荐
- datatime 模块
import datetime # 这个是一个包 里面包含 对时间的处理 对日期的处理datetime.date # 日期相关datetime.time # 时间相关 # 获取当前详细时间print( ...
- sqlserver查询父子级关系
自上向下的查询方法,查询出自身以及所有的子孙数据: --自上往下搜索 ;with maco as ( union all select t.* from ty_Dictionary t,maco m ...
- vue-6-事件处理
<div id="example-2"> <button v-on:click="greet">Greet</button> ...
- 服务器由于redis未授权访问漏洞被攻击
昨天阿里云拦截到了一次异常登陆,改了密码后就没有管他, 今天阿里云给我发消息说我的服务器可能被黑客利用,存在恶意发包行为....... 不过我不打算只是单纯的重置系统,经过一系列的查找原因后,发现被攻 ...
- (C/C++学习笔记) 二十四. 知识补充
二十四. 知识补充 ● 子类调用父类构造函数 ※ 为什么子类要调用父类的构造函数? 因为子类继承父类,会继承到父类中的数据,所以子类在进行对象初始化时,先调用父类的构造函数,这就是子类的实例化过程. ...
- Bluedroid: 音频数据的传输流程
一. UIPC: Audio Flinger获取到a2dp的hw module,然后蓝牙协议栈有专用于发送和接收media数据的线程,名称:btif_media_task. 蓝牙与Audio的 ...
- 1.5 socket服务器传输文件
socket服务器代码 # -*- coding: utf-8 -*-import sys,os,time,_thread from socket import * host = 'localhost ...
- centos7配置mysql
一:mysql安装方法一:yum安装 下载并安装MySQL官方的 Yum Repository https://dev.mysql.com/ cd ~ wget -i -c https://dev.m ...
- Beta阶段冲刺---Day4
一.Daily Scrum Meeting照片 二.今天冲刺情况反馈 昨天已完成的工作: (1)闯关模式排行榜代码编写: (2)闯关模式结束时的代码编写: (3)闯关模式开始时的代码编写. 今天计划完 ...
- kbmMWLog同时输出日志到多个日志管理器
kbmMWLog日志框架,针对不同的业务情况,提供了多种日志管理器: TkbmMWStreamLogManager TkbmMWLocalFileLogManager TkbmMWSystemLogM ...