转载请注明出处: 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. HEAP[xxx.exe]:Invalid Address specified to RtlValidateHeap 错误的解决方法总结

    一.情况 抽象出问题是这样的: class DLL_API1 A { func() { vector vec; B b; b.func(vec); return TRUE; } } 其中B是另一个导出 ...

  2. php正则验证手机号码

    protected function checkphone(){ if(preg_match("/^1[34578]\d{9}$/", $phone)){ return false ...

  3. 自定义View—绘制基本图形

    一.Canvas能够绘制哪些图形 二.

  4. 使用pip install 或者easy_install安装Python的各种包出现cc failed with exit status 1

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  5. Codeforces 372B Counting Rectangles is Fun

    http://codeforces.com/problemset/problem/372/B 题意:每次给出一个区间,求里面有多少个矩形 思路:预处理,sum[i][j][k][l]代表以k,l为右下 ...

  6. 《Programming WPF》翻译 第5章 3.命名属性

    原文:<Programming WPF>翻译 第5章 3.命名属性 通过把同样的内嵌样式提升到资源中(正如第一章介绍的),我们可以给它一个名字,以及按名字使用它在我们的Button实例上, ...

  7. WebDriver使用IE浏览器

    开始使用Selenium2之后就一直在用FireFox,因为文章上都说webdriver对firefox支持的最好,同时也很好上手,试了一下就可用了,也就没再用其他浏览器,不过最近遇到了一个问题,是我 ...

  8. MCM1988 问题B_lingo_装货问题

    两辆平板车的装货问题有七种规格的包装箱要装到两辆铁路平板车上去包装箱的宽和高是一样的但厚度(t,以厘米计)及重量(,以公斤计)是不同的.下表给出了每种包装箱的厚度重量以及数量每辆平板车有10.2 米长 ...

  9. Hdu5785-Interesting(回文串处理)

    Problem Description Alice get a string S. She thinks palindrome string is interesting. Now she wanna ...

  10. CharacterController 角色控制器实现移动和跳跃

    之前我使用SimpleMove来控制角色的移动, 后来又想实现人物的跳跃, 看见圣典里面是使用Move来实现的. =.= 然后我都把他们改成move来实现了 代码实现: using UnityEngi ...