转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Invitation Cards


Time Limit: 5 Seconds      Memory Limit: 65536 KB

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

题意:

有一个有n个点的有向图,从结点1派出n-1个人分别到剩余的n-1个点,然后再从这n-1个点回到结点1,求出其最短的路程总和

分析:

最短路模板题。

第一部分直接时从结点1出发的单元最短路,求一下和。剩下的一部分只要将原图的所有有向边的方向取反,在跑一遍最短路得出的结果就是。

注意,poj会卡vector,所以邻接表用vector会TLE,而且poj的数据会超int,要用long long

dij+heap代码

 #include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MAXN 1000010
typedef pair<int,int> PII;
typedef long long ll;
#define INF 0x3FFFFFFF
#define REP(X,N) for(int X=0;X<N;X++)
#define CLR(A,N) memset(A,N,sizeof(A))
#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A) vector<PII>G[MAXN];
vector<PII>rG[MAXN];
void add_edge(int u,int v,int d)
{
G[u].PB(MP(v,d));
rG[v].PB(MP(u,d));
}
void init(int p)
{
REP(i,p){
G[i].clear();
rG[i].clear();
}
}
int dis[MAXN];
bool vis[MAXN];
void dijkstra(int s)
{
REP(i,MAXN)dis[i]=i==s?:INF;
CLR(vis,);
priority_queue<PII,vector<PII>,greater<PII> >q; q.push(MP(dis[s],s));
while(!q.empty())
{
PII p=q.top();
q.pop();
int x=p.second;
if(vis[x])continue;
vis[x]=;
for(vector<PII>::iterator it=G[x].begin();it!=G[x].end();it++)
{
int y=it->first;
int d=it->second;
if(!vis[y]&&dis[x]+d<dis[y])
{
dis[y]=dis[x]+d;
q.push(MP(dis[y],y));
}
}
} }
void dijkstra1(int s)
{
REP(i,MAXN)dis[i]=i==s?:INF;
CLR(vis,);
priority_queue<PII,vector<PII>,greater<PII> >q;
q.push(MP(dis[s],s));
while(!q.empty())
{
PII p=q.top();
q.pop();
int x=p.second;
if(vis[x])continue;
vis[x]=;
for(vector<PII>::iterator it=rG[x].begin();it!=rG[x].end();it++)
{
int y=it->first;
int d=it->second;
if(!vis[y]&&dis[x]+d<dis[y])
{
dis[y]=dis[x]+d;
q.push(MP(dis[y],y));
}
}
} }
int main()
{
ios::sync_with_stdio(false);
int t;
scanf("%d",&t);
while(t--)
{
int p,q;
scanf("%d %d",&p,&q);
init(p);
int u,v,d;
REP(i,q){
scanf("%d%d%d",&u,&v,&d);
add_edge(u-,v-,d);
}
int ans=;
dijkstra();
REP(i,p){
ans+=dis[i];
}
dijkstra1();
REP(i,p){
ans+=dis[i];
}
cout<<ans<<endl;
}
return ;
}

代码君

spfa代码

 #include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <ios>
using namespace std;
#define PB(X) push_back(X)
#define REP(A,X) for(int A=0;A<X;A++)
#define MP(A,X) make_pair(A,X)
#define CLR(A,X) memset(A,X,sizeof(A))
typedef long long ll;
typedef pair<int,int > PII;
#define MAXN 1000010
/*vector<PII> G[MAXN];
vector<PII> rG[MAXN];*/
int head[MAXN],rhead[MAXN];
struct node{
int v,d,next;
}edge[*MAXN];//,redge[MAXN];
bool vis[MAXN];
#define INF 0x7FFFFFFF
int e=;
void add_edge(int u,int v,int d)
{
edge[e].d=d;
edge[e].next=head[u];
edge[e].v=v;
head[u]=e;
e++;
edge[e].d=d;
edge[e].next=rhead[v];
edge[e].v=u;
rhead[v]=e;
e++;
//G[u].PB(MP(v,d));
//rG[v].PB(MP(u,d));
}
///int p;
void init()
{
e=;
REP(i,MAXN)
{
head[i]=-;
rhead[i]=-;
//G[i].clear();
//rG[i].clear();
}
}
int dis[MAXN]; void spfa(int s)
{
REP(i,MAXN)dis[i]=i==s?:INF;
CLR(vis,);
queue<int>q;
while(!q.empty())q.pop();
q.push(s);
vis[s]=;
while(!q.empty())
{
int x=q.front();
q.pop();
//for(vector<PII>::iterator it=G[x].begin();it!=G[x].end();it++){
int t=head[x];
while(t!=-){
//int y=it->first;
//int d=it->second;
int y=edge[t].v;
int d=edge[t].d;
t=edge[t].next;
if(dis[x]+d<dis[y])
{
dis[y]=dis[x]+d;
if(vis[y])continue;
vis[y]=;
q.push(y);
}
}
vis[x]=;
}
}
void spfa1(int s)
{
REP(i,MAXN)dis[i]=i==s?:INF;
CLR(vis,);
queue<int>q;
while(!q.empty())q.pop();
q.push(s);
vis[s]=;
while(!q.empty())
{
int x=q.front();
q.pop();
//for(vector<PII>::iterator it=G[x].begin();it!=G[x].end();it++){
int t=rhead[x];
while(t!=-){
//int y=it->first;
//int d=it->second;
int y=edge[t].v;
int d=edge[t].d;
t=edge[t].next;
if(dis[x]+d<dis[y])
{
dis[y]=dis[x]+d;
if(vis[y])continue;
vis[y]=;
q.push(y);
}
}
vis[x]=;
}
}
int main()
{
ios::sync_with_stdio(false);
int t;
scanf("%d",&t);
ll ans;
int p,q;
int u,v,d;
while(t--)
{
scanf("%d%d",&p,&q);
init();
REP(i,q){
scanf("%d%d%d",&u,&v,&d);
add_edge(u-,v-,d);
}
spfa();
ans=;
REP(i,p)ans+=dis[i];
spfa1();
REP(i,p)ans+=dis[i];
printf("%lld\n",ans);
} return ;
}

代码君

poj1511/zoj2008 Invitation Cards(最短路模板题)的更多相关文章

  1. HDU 5521.Meeting 最短路模板题

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  2. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  3. 牛客小白月赛6 I 公交线路 最短路 模板题

    链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...

  4. POJ1511 Invitation Cards —— 最短路spfa

    题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Tota ...

  5. POJ-1511 Invitation Cards( 最短路,spfa )

    题目链接:http://poj.org/problem?id=1511 Description In the age of television, not many people attend the ...

  6. HDU 1535 Invitation Cards (最短路)

    题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...

  7. [USACO07FEB]银牛派对Silver Cow Party---最短路模板题

    银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...

  8. POJ 2387 Til the Cows Come Home --最短路模板题

    Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...

  9. hdu1874 最短路模板题

    之所以做了第二道模板题还要写是因为发现了一些自己的问题 用的是dij 最简单的松弛 需要注意的地方是松弛的时候 判断dis[i]<dis[w]+tance[w][i]时 还要再判断 vis[i] ...

随机推荐

  1. 用C++写出hanoi

    汉诺塔(港台:河內塔)是根据一个传说形成的數學问题有三根杆子A,B,C.A杆上有N个(N>1)穿孔圆盘,盘的尺寸由下到上依次变小.要求按下列规则将所有圆盘移至C杆:-每次只能移动一个圆盘-大的盘 ...

  2. winform textbox 的自动实现功能

    好久没写博客了,主要是太懒了,之前因为做bs的比较多现在想转cs端了,虽然现在做cs也一年了,可接触的东西太过零碎了,以至于感觉这一年好像什么都没有学到.估计是因为学了之后没有记录,不扎实,然后又忘记 ...

  3. angular--bootstrap实例日期控件【datepicker】

    head部分: <!--Bootstrap--> <link rel="stylesheet" href="/supProdom/script/boot ...

  4. javascript-ajax学习

    /**  *     @todo 封装Ajax 传输类  *     @param params:参数  *    @example 用法: var mAjaxer = new Ajaxer(para ...

  5. LeetCode_N-Queens II

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  6. js收集错误信息,错误上报

    线上的代码可有有时候用户会反应不好使,一般是因为js造成的! 尤其在移动端各个手机之前的差异特别大. 下面这段代码是获取能帮助你! <script> window.onerror = fu ...

  7. [置顶] Android EditText/TextView使用SpannableString显示复合文本

    在Android中EditText用于编辑文本,TextView用于显示文本,但是有时候我们需要对其中的文本进行样式等方面的设置.Android为我们提供了SpannableString类来对指定文本 ...

  8. 【转】gcc warning: braces around scalar initializer (标量初始化的括号)

    原文网址:http://stackoverflow.com/questions/3462513/gcc-warning-braces-around-scalar-initializer I have ...

  9. 原生javascript添加引用js文件

            function addScriptTag(src) {                         var script = document.createElement(&qu ...

  10. RCU 机制 [转IBM]

    2005 年 7 月 01 日 本文详细地介绍了 Linux 2.6 内核中新的锁机制 RCU(Read-Copy Update) 的实现机制,使用要求与典型应用. 一.引言 众所周知,为了保护共享数 ...