POJ 1511-Invitation Cards(SPFA)
Description
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
題目大意:一群人要幹什麼,然後在每個公交車站留人,有源點,求來回坐車最小花費。
思路:最近做最短路感覺做題不能被模板約束了,也不要侷限於模板,要學會他的思想與原理,才能做到真正的掌握這個演算法。
這個題很裸,求是所有點到源點的最短路之和。直接上板子就能做,spfa或者dij。用來試了一下spfa用棧來寫,結果發現自己對spfa的理解好像有點問題有扯到差分約束系統上去了,寫了後發現用棧與用佇列效率上沒什麼差別,但是在空間上有區別,此處考慮棧與佇列的性質。(2次SPFA跑出所有點的最短路累加)
還沒有試過spfa加前向星與LLL + SLF優化。遇到卡的再試呢還是抽空試呢,這是個問題.....
程式碼如下:
#include<set>
#include<map>
#include<list>
#include<deque>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<stdio.h>
#include<sstream>
#include<stdlib.h>
#include<string.h>
//#include<ext/rope>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
#define per(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
#define swap(a,b) {int t=a;a=b;b=t}
using namespace std;
//using namespace __gnu_cxx;
#define N 1000010
LL d[N];
int n,m;
int head[N];
int vis[N];
struct node{
int u;
int v;
int w;
int next;
}p[N],t[N];
void add(int u,int v,int w,int k)
{
p[k].u=u;
p[k].v=v;
p[k].w=w;//權
p[k].next=head[u];//出發點
head[u]=k;
}
LL spfa_stack()//先進先出
{
stack<int>q;
memset(d,INF,sizeof(d));
memset(vis,0,sizeof(vis));
while(!q.empty()) q.pop();
d[1]=0;
vis[1]=1;
q.push(1);
while(!q.empty())
{
int u=q.top();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=p[i].next)//類似連結串列實現遍歷該點的所有邊
{
int v=p[i].v;
int w=p[i].w;
if(d[u]+w<d[v]&&vis[v]==0)
{
d[v]=d[u]+w;
q.push(v);
}
}
}
LL s=0;
per(i,1,n) s+=d[i];
return s;
}
int main()
{
int T,a,b,c;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
per(i,0,m-1)
{
scanf("%d%d%d",&a,&b,&c);
t[i].u=a;
t[i].v=b;
t[i].w=c;
add(a,b,c,i);
}
LL s1=spfa_stack();
memset(head,-1,sizeof(head));
per(i,0,m-1) add(t[i].v,t[i].u,t[i].w,i);
LL s2=spfa_stack();
printf("%lld\n",s1+s2);
}
return 0;
}
相關文章
- POJ 1511 Invitation Cards(最短路spfa演算法)演算法
- POJ 1724 ROADS(優先佇列+spfa)佇列
- POJ 3592 Instantaneous Transference 圖論演算法tarjan+spfa圖論演算法
- POJ1511 Invitation Cards【Dijkstra+堆優化+前向星】優化
- POJ 3159-Candies(差分約束系統-SPFA+鄰接表)
- SPFA演算法演算法
- spfa最佳化
- SPFA && dijkstra 模版
- 【Lintcode】1728. X of a Kind in a Deck of Cards
- CARDS主題 & 導航欄樣式修改
- HDU 4460 Friend Chains(map + spfa)AI
- Throwing cards away I(queue迴圈佇列)佇列
- SPFA演算法模板(C/C++)演算法C++
- 最短路(DJsktra,spfa,flyd).mdJS
- LeetCode之Reveal Cards In Increasing Order(Kotlin)LeetCodeKotlin
- Codeforces Round #235 (Div. 2) A. Vanya and Cards
- 最短路-SPFA演算法&Floyd演算法演算法
- POJ 3461 kmpKMP
- poj3417
- 最短路演算法詳解(Dijkstra/SPFA/Floyd)演算法
- 「學習筆記」SPFA 演算法的最佳化筆記演算法
- hdu 4568 spfa 最短路演算法+旅行商問題演算法
- Flip Game(POJ 1753)GAM
- POJ 2431 Expedition
- POJ3259-WormholesWorm
- POJ 3414 Pots
- P1446-[HNOI2008]Cards【Burnside引理,dp】IDE
- POJ 1562 Oil Deposits
- poj 2478 尤拉函式函式
- POJ 2524 Ubiquitous ReligionsUI
- POJ 3981 字串替換字串
- poj 1276 Cash MachineMac
- POJ 基本演算法演算法
- POJ3414-Pots
- POJ 1089 Intervals
- 牛客網暑期ACM多校訓練營(第三場)C Shuffle Cards(splay)ACM
- POJ 2799 IP Networks
- POJ 2891 Strange Way to Express IntegersExpress