Description

In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery.
The transport system is very special: all lines are
unidirectional and connect exactly two stops. Buses leave the
originating stop with passangers each half an hour. After reaching the
destination stop they return empty to the originating stop, where they
wait until the next full half an hour, e.g. X:00 or X:30, where 'X'
denotes the hour. The fee for transport between two stops is given by
special tables and is payable on the spot. The lines are planned in such
a way, that each round trip (i.e. a journey starting and finishing at
the same stop) passes through a Central Checkpoint Stop (CCS) where each
passenger has to pass a thorough check including body scan.

All the ACM student members leave the CCS each morning.
Each volunteer is to move to one predetermined stop to invite
passengers. There are as many volunteers as stops. At the end of the
day, all students travel back to CCS. You are to write a computer
program that helps ACM to minimize the amount of money to pay every day
for the transport of their employees.

Input

The input consists of N cases. The first line of the input contains
only positive integer N. Then follow the cases. Each case begins with a
line containing exactly two integers P and Q, 1 <= P,Q <= 1000000.
P is the number of stops including CCS and Q the number of bus lines.
Then there are Q lines, each describing one bus line. Each of the lines
contains exactly three numbers - the originating stop, the destination
stop and the price. The CCS is designated by number 1. Prices are
positive integers the sum of which is smaller than 1000000000. You can
also assume it is always possible to get from any stop to any other
stop.
 

Output

For each case, print one line containing the minimum amount of money to
be paid each day by ACM for the travel costs of its volunteers.
 

Sample Input

2
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50

Sample Output

46
210
 
题目大意:单向边,求从起点1到每个点的最短路然后再回到起点1的最短路之和。
题目解析:朴素算法TLE。求每个点到起点的最短时,将每一条边倒过来,又转化成了起点1到每一个点的距离之和。(逆向思维)
 
代码如下:
 # include<iostream>
# include<cstdio>
# include<cstring>
# include<queue>
# include<algorithm>
using namespace std;
const int N=;
const long long INF=<<;
struct edge
{
int to,w,nxt;
};
edge e[N+];
int n,cnt,head[N+],a1[N+],a2[N+],c[N+];
long long dis[N+];
void add(int u,int v,int w)
{
e[cnt].to=v;
e[cnt].w=w;
e[cnt].nxt=head[u];
head[u]=cnt++;
}
long long spfa()
{
fill(dis,dis+n+,INF);
queue<int>q;
q.push();
dis[]=;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=e[i].nxt){
if(dis[e[i].to]>dis[u]+e[i].w){
dis[e[i].to]=dis[u]+e[i].w;
q.push(e[i].to);
}
}
}
long long res=;
for(int i=;i<=n;++i)
res+=dis[i];
return res;
}
int main()
{
int T,m;
scanf("%d",&T);
while(T--)
{
cnt=;
scanf("%d%d",&n,&m);
fill(head,head+n+,-);
for(int i=;i<m;++i){
scanf("%d%d%d",&a1[i],&a2[i],&c[i]);
add(a1[i],a2[i],c[i]);
}
long long ans=spfa();
//cout<<ans<<endl;
cnt=;
fill(head,head+n+,-);
for(int i=;i<m;++i)
add(a2[i],a1[i],c[i]);
ans+=spfa();
printf("%lld\n",ans);
}
return ;
}

POJ-1511 Invitation Cards (双向单源最短路)的更多相关文章

  1. POJ 1511 Invitation Cards ( 双向单源最短路 || 最小来回花费 )

    题意 : 给出 P 个顶点以及 Q 条有向边,求第一个点到其他各点距离之和+其他各点到第一个点的距离之和的最小值 分析 : 不难看出 min( 第一个点到其他各点距离之和+其他各点到第一个点的距离之和 ...

  2. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

  3. POJ-1511 Invitation Cards (单源最短路+逆向)

    <题目链接> 题目大意: 有向图,求从起点1到每个点的最短路然后再回到起点1的最短路之和. 解题分析: 在求每个点到1点的最短路径时,如果仅仅只是遍历每个点,对它们每一个都进行一次最短路算 ...

  4. 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 / ...

  5. poj 1511 Invitation Cards (最短路)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 33435   Accepted: 111 ...

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

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

  7. [POJ] 1511 Invitation Cards

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 18198   Accepted: 596 ...

  8. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

  9. SPFA算法(2) POJ 1511 Invitation Cards

    原题: Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 31230   Accepted: ...

随机推荐

  1. Python入门之Python Colorama模块

    Python的Colorama模块,可以跨多终端,显示字体不同的颜色和背景,只需要导入colorama模块即可,不用再每次都像linux一样指定颜色: 官方参考:https://pypi.org/pr ...

  2. mariadb 10.1查看per connection内存消耗

    在mariadb 10.1版本中,在information_schema.processlist表中,新增了几个字段,其中有一个memory_used,其记录的是连接的内存消耗. 同时新增了一个状态变 ...

  3. 03:requests与BeautifulSoup结合爬取网页数据应用

    1.1 爬虫相关模块命令回顾 1.requests模块 1. pip install requests 2. response = requests.get('http://www.baidu.com ...

  4. 20145206邹京儒Exp6 信息搜集与漏洞扫描

    20145206邹京儒Exp6 信息搜集与漏洞扫描 一.实践过程记录 openvas漏洞扫描 1.openvas-check-setup来查看下他的安装状态: 如下图所示:在步骤7中出现错误,显示需要 ...

  5. 开源项目托管GitHub

    工具:本地HelloWorld源项目 msysgit(Windows) 实验步骤:一.在GitHub新建托管项目 在http://github.com注册账号20159214-sunnan. 完成注册 ...

  6. Java实现心跳机制

    一.心跳机制简介 在分布式系统中,分布在不同主机上的节点需要检测其他节点的状态,如服务器节点需要检测从节点是否失效.为了检测对方节点的有效性,每隔固定时间就发送一个固定信息给对方,对方回复一个固定信息 ...

  7. BZOJ1045 [HAOI2008]糖果传递 && BZOJ3293 [Cqoi2011]分金币

    Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数nn<=1'000'000,表示小朋友的个 ...

  8. github上不去了

    这几天发现github.com上不去了 可能是由于要杜绝国外的人使用最新的技术??从而屏蔽了吗?

  9. Unity3D学习笔记(十九):UGUI、Image、Text、Button

    UGUI:Unity官方最新,与NGUI同源 UI:User Interface(用户的操作界面),图片+文字 UGUI的组件: 1.创建UGUI组件时,会默认创建Canvas(画布)和EventSy ...

  10. UVa 1471 防线

    https://vjudge.net/problem/UVA-1471 题意:给出一个序列,删除一个连续子序列,使得剩下的序列中有一个长度最大的连续递增子序列,输出个数. 思路:首先可以计算出以i结尾 ...