转载请注明出处: 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. VC连接数据库方式

    转自:http://www.cnblogs.com/renyuan/archive/2012/07/27/2612412.html 目前Windows系统上常见的数据库接口包括: ODBC(开放数据库 ...

  2. [FML]学习笔记一Cross-validation交叉验证

    在实际的工程中,有时labeled data的数量不足以留出validation sample(验证样本)否则会导致training sample(训练样本)的数量太少.为了解决这个问题,我们引入一种 ...

  3. PHP Warning: phpinfo(): It is not safe to rely on the system's timezone setting

    错误描述: PHP Warning:  phpinfo(): It is not safe to rely on the system's timezone settings. You are *re ...

  4. Hdu1095

    #include <stdio.h> int main() { int a,b; while(scanf("%d %d",&a,&b)!=EOF){ p ...

  5. Virtual Box 工具栏(菜单栏)消失的解决方法

    异常处理汇总-开发工具  http://www.cnblogs.com/dunitian/p/4522988.html 现在Virtual Box非常牛逼(不排除Oracle又准备像Java SE那样 ...

  6. Objective-C中release和nil的关系

    (iphone/ipad)浅谈Objective-C中release和nil的关系 分类: iPhone/iPad开发技术2011-12-09 01:40 2515人阅读 评论(4) 收藏 举报 ui ...

  7. 开心菜鸟系列学习笔记--------初探Nodejs(了解篇)

    一Node.js开始学习了!    1) 输出hellow worlds   a.建一个js文件 hello.js 写 console.info('hellow world !!!');    进入终 ...

  8. 本地windows主机无法访问虚拟机里主机解决办法

    一:设置虚拟机里IP,使其与本地计算机IP在同一网段 本地计算机网络IP设置如下: 虚拟机里ip为192.168.1.9 设置IP步骤请参考:Linux里如何设置IP(RED HAT) 二:将虚拟机网 ...

  9. JVM性能调优博客

    http://houjixin.blog.163.com/blog/static/35628410201411275719843/ http://blog.csdn.net/lastsweetop/a ...

  10. rsyslog VS syslog-ng,日志记录哪家强?

    还有慢慢摸索,NG的MYSQL配置,我始终没搞好. RSYSLOG则比较容易. 另外,也可以每个RSYSLOG直接入库,不需要经过LOG SERVER..如果有一个大内网的话... 配合LOGANAL ...