poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: 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(最短路模板题)的更多相关文章
- HDU 5521.Meeting 最短路模板题
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- 牛客小白月赛6 I 公交线路 最短路 模板题
链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...
- POJ1511 Invitation Cards —— 最短路spfa
题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Tota ...
- POJ-1511 Invitation Cards( 最短路,spfa )
题目链接:http://poj.org/problem?id=1511 Description In the age of television, not many people attend the ...
- HDU 1535 Invitation Cards (最短路)
题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...
- [USACO07FEB]银牛派对Silver Cow Party---最短路模板题
银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- hdu1874 最短路模板题
之所以做了第二道模板题还要写是因为发现了一些自己的问题 用的是dij 最简单的松弛 需要注意的地方是松弛的时候 判断dis[i]<dis[w]+tance[w][i]时 还要再判断 vis[i] ...
随机推荐
- 解决Sublime-Text-3在ubuntu下中文输入的问题
在ubuntu下使用ST这神器已经一段日子了,但是一直有个纠结的问题,就是中文输入非常坑爹,曾经一段时间,使用inputHelper这个插件来解决, 但是……每次都要按个快捷键,弹出一个小小小框来输入 ...
- AttributeError at /home/home/ Exception Type: AttributeError at /home/home/
"错误提示信息": Environment: Request Method: GET Request URL: http://localhost:8000/home/home/ D ...
- javascript中0级DOM和2级DOM事件模型浅析
Javascript程序使用的是事件驱动的设计模式,为一个元素添加事件监听函数,当这个元素的相应事件被触发那么其添加的事件监听函数就被调用: <input type="button&q ...
- iOS学习之详解AppDelegate
AppDelegate, 类似于监听接口. 用个很简单的例子说:ios系统会控制每个程序的开始和结束.但是ios又不知道每个程序的开始需要运行成么代码,结束需要运行什么代码.这个时候,ios就制定了一 ...
- nodeclub 学习记录
源码地址:https://github.com/cnodejs/nodeclub 按照 它的步骤 在系统中跑没有出错,但是注册后没有发送邮件验证码,我将 controller层下面的sign.js 的 ...
- 【破解】破解ACDSEE15的方法
1.先从官方下载一个ACDSEE15简体中文版 2.下载注册机(点我下载) 3.修改注册表 修改注册表ACDSee 32位:HKEY_LOCAL_MACHINE\SOFTWARE\ACD System ...
- XJOI网上同步训练DAY1 T3
思路:一开始看到这题的时候想DP,可是发现貌似不行..因为有前缀也有后缀,而且有的后缀会覆盖到现在的前缀,这就不满足无后效性了啊! 但是有个很巧妙的思路:如果我们知道a[i]的最大值,那么p的数量和q ...
- Ubuntu下配置NFS服务
Table of Contents 1.下载相关软件 2.建立共享目录 3.修改该配置文件 4.重启服务 5.测试服务器 6.测试客户端 测试系统:Ubuntu8.04 1.下载相关软件 使用如下命令 ...
- 继承Object和ContextBoundObject处理效率上的差距
继承Object和ContextBoundObject处理效率上的差距 ContextBoundObject一个很熟悉的对象,它提供对象有处理上下文的能力:通过它能够方便地实现AOP处理机制.它带来好 ...
- 【转】 i2c驱动调试经验
原文网址:http://blog.csdn.net/cmm20071020/article/details/7179958 把一个i2c驱动从2.6.21升级到2.6.39 上网查到一篇帖子,讲了驱动 ...