HDU 1535
http://acm.hdu.edu.cn/showproblem.php?pid=1535
水题
单向图,从1到P所有点,再从所有点回到1,问最小花费
先求一遍1的最短路,然后反向建图,再求一遍1的最短路
#include <iostream>
#include <queue>
using namespace std ;
const int INF=0xfffffff ;
struct node{
int s,t,v,nxt ;
}e[] ;
int n,cnt,head[],vis[],dis[] ;
int rs[],rt[],rv[] ;
void add(int s,int t,int v)
{
e[cnt].s=s ;
e[cnt].t=t ;
e[cnt].v=v ;
e[cnt].nxt=head[s] ;
head[s]=cnt++ ;
}
void spfa(int s)
{
for(int i= ;i<=n ;i++)
dis[i]=INF ;
dis[s]= ;
memset(vis,,sizeof(vis)) ;
vis[s]= ;
queue <int> q ;
q.push(s) ;
while(!q.empty())
{
int u=q.front() ;
q.pop() ;
vis[u]= ;
for(int i=head[u] ;i!=- ;i=e[i].nxt)
{
int tt=e[i].t ;
if(dis[tt]>dis[u]+e[i].v)
{
dis[tt]=dis[u]+e[i].v ;
if(!vis[tt])
{
vis[tt]= ;
q.push(tt) ;
}
}
}
}
}
int main()
{
int t ;
scanf("%d",&t) ;
while(t--)
{
cnt= ;
memset(head,-,sizeof(head)) ;
int q ;
scanf("%d%d",&n,&q) ;
for(int i= ;i<q ;i++)
{
scanf("%d%d%d",&rs[i],&rt[i],&rv[i]) ;
add(rs[i],rt[i],rv[i]) ;
}
int ans= ;
spfa() ;
for(int i= ;i<=n ;i++)
ans+=dis[i] ;
cnt= ;
memset(head,-,sizeof(head)) ;
for(int i= ;i<q ;i++)
add(rt[i],rs[i],rv[i]) ;
spfa() ;
for(int i= ;i<=n ;i++)
ans+=dis[i] ;
printf("%d\n",ans) ;
}
return ;
}
HDU 1535的更多相关文章
- HDU 1535 Invitation Cards (POJ 1511)
两次SPFA. 求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换.可是这个有1000000个点.矩阵开不了. d1[]为 1~N 的最短路. 将全部边的 邻点 交换. d ...
- 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 / ...
- hdu 1535 Invitation Cards
http://acm.hdu.edu.cn/showproblem.php?pid=1535 这道题两遍spfa,第一遍sfpa之后,重新建图,所有的边逆向建边,再一次spfa就可以了. #inclu ...
- HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...
- HDU 1535 Invitation Cards(最短路 spfa)
题目链接: 传送门 Invitation Cards Time Limit: 5000MS Memory Limit: 32768 K Description In the age of te ...
- [HDU 1535]Invitation Cards[SPFA反向思维]
题意: (欧洲人自己写的题面就是不一样啊...各种吐槽...果断还是看晕了) 有向图, 有个源叫CCS, 求从CCS到其他所有点的最短路之和, 以及从其他所有点到CCS的最短路之和. 思路: 返回的时 ...
- HDU 1535 Invitation Cards(SPFA,及其优化)
题意: 有编号1-P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费. 有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1. ...
- hdu 1535 Invitation Cards(SPFA)
Invitation Cards Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) T ...
- HDU 1535 Invitation Cards (最短路)
题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...
随机推荐
- 前端迭代取出 后台map返回的数据
<body> <div th:each="map : ${response}"> <span th:text="${map.key}&quo ...
- php5.6安装gd库
rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -Uvh ht ...
- VS2010/MFC编程入门之二十九(常用控件:列表视图控件List Control 下)
上一节是关于列表视图控件List Control的上半部分,简单介绍了列表视图控件,其通知消息的处理和有关结构体的定义.本节继续讲解下半部分,包括列表视图控件的创建.CListCtrl类的主要成员函数 ...
- CCPC 2016-2017, Finals Solution
A - The Third Cup is Free 水. #include<bits/stdc++.h> using namespace std; ; int n; int arr[max ...
- 【android】如何实现猿题库题目的排版
最近我们的产品来了个新的模块,类似猿题库一样,给学生做题提高成绩的. 要求如下: 1:支持单选.多选.填空题 2:支持图片文字混排 3:输入框有交互,排版精致美观 4:为了体验优化,不能使用网页实现效 ...
- python webdriver grid多节点运行webdriver程序
grid整理: 机制 Hub机器和节点机器上要装jdk和jar包 A机器:hub 中控:用来监控所有节点机的状态 启动命令: java -jar selenium-server-standalone ...
- C++ string和C风格字符串
https://msdn.microsoft.com/en-us/library/syxtdd4f.aspx#basic_string__replace If you need to convert ...
- linux时区问题
时区问题很麻烦- 0.查看时间命令 #date 查看系统时间 #date -s 修改时间,看下面的例子 #// (将系统日期设定为2014年07月16日) #:: (将系统时间设定为下午11::) # ...
- 在pom.xml中使用distributionManagement将项目打包上传到nexus私服
本文介绍 如何在pom.xml中使用distributionManagement将项目打包上传到nexus私服 1.pom.xml文件添加distributionManagement节点 <!- ...
- (转载)Ubuntu 16.04+1080Ti机器学习基本环境配置
转载自:https://blog.csdn.net/mahonesun/article/details/80808930 一.设置网络 机器有两张网卡,将当前正在使用的"有线连接1" ...