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. OpenGL边用边学------2 经典照相机模型

    https://blog.csdn.net/smstong/article/details/50290327 实际照相步骤 1 布置场景和调整照相机位置 3 选择镜头对焦Focus 4 按下快门 5 ...

  2. Low-level Native Plugin Interface

    http://docs.manew.com/Manual/index.htm https://docs.unity3d.com/Manual/NativePluginInterface.html ht ...

  3. MySQL Crash Course #06# Chapter 13. 14 GROUP BY. 子查询

    索引 理解 GROUP BY 过滤数据 vs. 过滤分组 GROUP BY 与 ORDER BY 之不成文的规定 子查询 vs. 联表查询 相关子查询和不相关子查询. 增量构造复杂查询 Always ...

  4. java在访问https资源时的证书信任问题

    java程序在访问https资源时,出现报错 sun.security.validator.ValidatorException: PKIX path building failed: sun.sec ...

  5. 常用模块之 shutil,json,pickle,shelve,xml,configparser

    shutil 高级的文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中 import shutil shut ...

  6. Pandas fillna('Missing')

    https://blog.csdn.net/donghf1989/article/details/51167083/ .使用0替代缺失值(当然你可以用任意一个数字代替NaN) df.fillna(0) ...

  7. junit中test注解测试使用案列解析一

    本文原创,转载请注明出处 在写代码的过程中,只想测试部分代码,调试一小段功能有没有通的情况下,可以用该方法: 以下为在项目中测试一个小功能的案例,在此记录一下, /**     * <解析查询磁 ...

  8. springmvc基础知识及注解

    SpringMVC 1.概念 Spring的MVC框架是一个基于DispatcherServlet的MVC框架,主要由DispatcherServlet.处理器映射.处理器.视图解析器.视图组成.每一 ...

  9. LA 3486 Cells(判祖先+栈模拟dfs)

    https://vjudge.net/problem/UVALive-3486 题意: 判断u是否是v的祖先. 思路: 很简单,dfs遍历,记录每个节点第一次访问时的时间戳 in[i] 和第二次访问时 ...

  10. UVa 1614 奇怪的股市

    https://vjudge.net/problem/UVA-1614 题意:输入一个长度为n的序列a,满足1<=ai<=i,要求确定每个数的正负号,使得所有数的总和为0. 思路:贪心部分 ...