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] ...
随机推荐
- lightoj 1236 正整数唯一分解定理
A - (例题)整数分解 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768KB 6 ...
- 洛谷 P3383 【模板】线性筛素数
P3383 [模板]线性筛素数 题目描述 如题,给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内) 输入输出格式 输入格式: 第一行包含两个正整数N.M,分别表示查询的范 ...
- xen之基本搭建
1. 前言 所需包: kernel-xen xen xen-libs (xen依赖包) xen_runtime (xen依赖包) 以上xen包需要版本号一致,例如4.1.3版本,这里使用xm接口管理工 ...
- 【7】使用css/js/html模板来实现一个注册、登录和管理的功能
分支:auth static添加文件 css文件夹: app.css 自定义css样式[*] bootstrap.min.cs bootstrap样式 compomemts文件夹: 插件用 ...
- 数值运算内建函数(core python programming 2nd edition 5.6.2)
数值运算内建函数 函数 功能 abs(num) 返回 num 的绝对值 coerce(num1, num2) 将num1和num2转换为同一类型,然后以一个元组的形式返回. divmod(num1, ...
- Hdu1093
#include <stdio.h> int main() { int T,n; ; while(scanf("%d",&T)!=EOF){ while(sca ...
- C51工具是怎么进行覆盖分析的
C51工具针对8051微控制器的有限存储器资源进行了优化设计. 为了最有效地利用存储器,根据一个很容易解释的方法,自动变量和函数参数在存储器中均进行覆盖处理. 首先,连接器根据源程序生成调用树.例如: ...
- qt之fillder抓包(QT网络版有一些具体的坑)
最近项目中使用到了Qt的网络库,在用的过程中也发现了不少坑和问题,本文仅仅作为记录,方便日后查阅. 因为我们整个客户端的gui都是使用qt来完成的,心想qt既然有网络库,而且真心觉着qt封装的控 ...
- 团队介绍 | 魅动 Magic Motion
团队介绍 | 魅动 Magic Motion 魅动 Magic Motion 是专注于研发.设计和生产智能化私密生活用品的创新品牌.魅动 Magic Motion 将无线移动通信技术与私密生活产品相结 ...
- <转载>模板声明中template <typename T>和template <class T>
原文地址http://blog.csdn.net/bug07250432/article/details/10150625 在c++Template中很多地方都用到了typename与class这两个 ...