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 ...
随机推荐
- day30 操作系统介绍 进程的创建
今日内容 一.操作系统的简单介绍 二,并发与并行 三.同步异步阻塞非阻塞 四.multiprocess模块 1. 操作系统的简单介绍 多道技术(重点) 空间复用: 时间复用: 进程之间是空间隔离的 分 ...
- S2 深入.NET和C#编程 机试测试错题积累
机试第一题: //创建文件流(路径 模式) FileStream fa = new FileStream(@"D:\\S2第一次全文 ...
- flask小例
#写一个app.py,处理3个URL: ''' GET / : 首页,返回Home; GET /signin:登录页,显示登录表单; POST /signin: 处理登录表单,显示登录结果. ''' ...
- MySQL输入密码后闪退
刚刚我遇到这个问题,服务里MySQL是启状态的,所以我求助百度,发现很多种说法,我试了几个,还是不行,后来想起来我的密码不对,于是换了正确的密码试了一下,没毛病,进去了. 所以输入密码闪退时,首先确定 ...
- HTTP网页过程
HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: (1) 建立TCP连接 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立 ...
- MySql笔记之数据备份与还原
MySQL数据备份.还原与迁移 一.数据备份------mysqldump 1.语法: mysqldump -u user(用户名)-h host(登录用户的主机名称)-p password(登录密码 ...
- spring 事务控制 设置手动回滚 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//假设这是一个service类的片段 try{ //出现异常 } catch (Exception e) { e.printStackTrace(); //设置手动回滚 TransactionAsp ...
- Spring Boot 揭秘与实战(七) 实用技术篇 - FreeMarker 模板引擎
文章目录 1. FreeMaker 代替 JSP 作为页面渲染 2. 生成静态文件 3. 扩展阅读 4. 源代码 Spring Boot 提供了很多模板引擎的支持,例如 FreeMarker.Thym ...
- React Natived打包报错java.io.IOException: Could not delete path '...\android\support\v7'解决
问题详情 React Native打包apk时在第二次编译时候报错: java.io.IOException: Could not delete path 'D:\mycode\reactnative ...
- [转] Haproxy、Keepalived双主高可用负载均衡
http://blog.chinaunix.net/uid-25266990-id-3989321.html 在测试了Nginx+Keepalived的负载均衡后,也对Haproxy+Keepaliv ...