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代码

  1 #include <iostream>
  2 #include <vector>
  3 #include <algorithm>
  4 #include <cstdio>
  5 #include <queue>
  6 #include <cstdlib>
  7 #include <cstring>
  8 using namespace std;
  9 #define MAXN 1000010
 10 typedef pair<int,int> PII;
 11 typedef long long ll;
 12 #define INF 0x3FFFFFFF
 13 #define REP(X,N) for(int X=0;X<N;X++)
 14 #define CLR(A,N) memset(A,N,sizeof(A))
 15 #define MP(A,B) make_pair(A,B)
 16 #define PB(A) push_back(A)
 17
 18 vector<PII>G[MAXN];
 19 vector<PII>rG[MAXN];
 20 void add_edge(int u,int v,int d)
 21 {
 22     G[u].PB(MP(v,d));
 23     rG[v].PB(MP(u,d));
 24 }
 25 void init(int p)
 26 {
 27     REP(i,p){
 28         G[i].clear();
 29         rG[i].clear();
 30     }
 31 }
 32 int dis[MAXN];
 33 bool vis[MAXN];
 34 void dijkstra(int s)
 35 {
 36     REP(i,MAXN)dis[i]=i==s?0:INF;
 37     CLR(vis,0);
 38     priority_queue<PII,vector<PII>,greater<PII> >q;
 39
 40     q.push(MP(dis[s],s));
 41     while(!q.empty())
 42     {
 43         PII p=q.top();
 44         q.pop();
 45         int x=p.second;
 46         if(vis[x])continue;
 47         vis[x]=1;
 48         for(vector<PII>::iterator it=G[x].begin();it!=G[x].end();it++)
 49         {
 50             int y=it->first;
 51             int d=it->second;
 52             if(!vis[y]&&dis[x]+d<dis[y])
 53             {
 54                 dis[y]=dis[x]+d;
 55                 q.push(MP(dis[y],y));
 56             }
 57         }
 58     }
 59
 60 }
 61 void dijkstra1(int s)
 62 {
 63     REP(i,MAXN)dis[i]=i==s?0:INF;
 64     CLR(vis,0);
 65     priority_queue<PII,vector<PII>,greater<PII> >q;
 66     q.push(MP(dis[s],s));
 67     while(!q.empty())
 68     {
 69         PII p=q.top();
 70         q.pop();
 71         int x=p.second;
 72         if(vis[x])continue;
 73         vis[x]=1;
 74         for(vector<PII>::iterator it=rG[x].begin();it!=rG[x].end();it++)
 75         {
 76             int y=it->first;
 77             int d=it->second;
 78             if(!vis[y]&&dis[x]+d<dis[y])
 79             {
 80                 dis[y]=dis[x]+d;
 81                 q.push(MP(dis[y],y));
 82             }
 83         }
 84     }
 85
 86 }
 87 int main()
 88 {
 89     ios::sync_with_stdio(false);
 90     int t;
 91     scanf("%d",&t);
 92     while(t--)
 93     {
 94         int p,q;
 95         scanf("%d %d",&p,&q);
 96         init(p);
 97         int u,v,d;
 98         REP(i,q){
 99             scanf("%d%d%d",&u,&v,&d);
100             add_edge(u-1,v-1,d);
101         }
102         int ans=0;
103         dijkstra(0);
104         REP(i,p){
105             ans+=dis[i];
106         }
107         dijkstra1(0);
108         REP(i,p){
109             ans+=dis[i];
110         }
111         cout<<ans<<endl;
112     }
113     return 0;
114 }

代码君

spfa代码

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <queue>
  4 #include <vector>
  5 #include <cstring>
  6 #include <cstdlib>
  7 #include <algorithm>
  8 #include <ios>
  9 using namespace std;
 10 #define PB(X) push_back(X)
 11 #define REP(A,X) for(int A=0;A<X;A++)
 12 #define MP(A,X) make_pair(A,X)
 13 #define CLR(A,X) memset(A,X,sizeof(A))
 14 typedef long long ll;
 15 typedef pair<int,int > PII;
 16 #define MAXN 1000010
 17 /*vector<PII> G[MAXN];
 18 vector<PII> rG[MAXN];*/
 19 int head[MAXN],rhead[MAXN];
 20 struct node{
 21     int v,d,next;
 22 }edge[2*MAXN];//,redge[MAXN];
 23 bool vis[MAXN];
 24 #define INF 0x7FFFFFFF
 25 int e=0;
 26 void add_edge(int u,int v,int d)
 27 {
 28     edge[e].d=d;
 29     edge[e].next=head[u];
 30     edge[e].v=v;
 31     head[u]=e;
 32     e++;
 33     edge[e].d=d;
 34     edge[e].next=rhead[v];
 35     edge[e].v=u;
 36     rhead[v]=e;
 37     e++;
 38     //G[u].PB(MP(v,d));
 39     //rG[v].PB(MP(u,d));
 40 }
 41 ///int p;
 42 void init()
 43 {
 44     e=0;
 45     REP(i,MAXN)
 46     {
 47         head[i]=-1;
 48         rhead[i]=-1;
 49         //G[i].clear();
 50         //rG[i].clear();
 51     }
 52 }
 53 int dis[MAXN];
 54
 55 void spfa(int s)
 56 {
 57     REP(i,MAXN)dis[i]=i==s?0:INF;
 58     CLR(vis,0);
 59     queue<int>q;
 60     while(!q.empty())q.pop();
 61     q.push(s);
 62     vis[s]=1;
 63     while(!q.empty())
 64     {
 65         int x=q.front();
 66         q.pop();
 67         //for(vector<PII>::iterator it=G[x].begin();it!=G[x].end();it++){
 68         int t=head[x];
 69         while(t!=-1){
 70             //int y=it->first;
 71             //int d=it->second;
 72             int y=edge[t].v;
 73             int d=edge[t].d;
 74             t=edge[t].next;
 75             if(dis[x]+d<dis[y])
 76             {
 77                 dis[y]=dis[x]+d;
 78                 if(vis[y])continue;
 79                 vis[y]=1;
 80                 q.push(y);
 81             }
 82         }
 83         vis[x]=0;
 84     }
 85 }
 86 void spfa1(int s)
 87 {
 88     REP(i,MAXN)dis[i]=i==s?0:INF;
 89     CLR(vis,0);
 90     queue<int>q;
 91     while(!q.empty())q.pop();
 92     q.push(s);
 93     vis[s]=1;
 94     while(!q.empty())
 95     {
 96         int x=q.front();
 97         q.pop();
 98         //for(vector<PII>::iterator it=G[x].begin();it!=G[x].end();it++){
 99         int t=rhead[x];
100         while(t!=-1){
101             //int y=it->first;
102             //int d=it->second;
103             int y=edge[t].v;
104             int d=edge[t].d;
105             t=edge[t].next;
106             if(dis[x]+d<dis[y])
107             {
108                 dis[y]=dis[x]+d;
109                 if(vis[y])continue;
110                 vis[y]=1;
111                 q.push(y);
112             }
113         }
114         vis[x]=0;
115     }
116 }
117 int main()
118 {
119     ios::sync_with_stdio(false);
120     int t;
121     scanf("%d",&t);
122     ll ans;
123     int p,q;
124     int u,v,d;
125     while(t--)
126     {
127         scanf("%d%d",&p,&q);
128         init();
129         REP(i,q){
130             scanf("%d%d%d",&u,&v,&d);
131             add_edge(u-1,v-1,d);
132         }
133         spfa(0);
134         ans=0;
135         REP(i,p)ans+=dis[i];
136         spfa1(0);
137         REP(i,p)ans+=dis[i];
138         printf("%lld\n",ans);
139     }
140
141     return 0;
142 }

代码君

时间: 2024-10-15 12:09:19

poj1511/zoj2008 Invitation Cards(最短路模板题)的相关文章

POJ 1511 Invitation Cards (最短路)

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 19215   Accepted: 6311 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They wan

HDU 5521.Meeting 最短路模板题

Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3361    Accepted Submission(s): 1073 Problem Description Bessie and her friend Elsie decide to have a meeting. However, after Farmer Jo

[poj2449]Remmarguts&#39; Date(K短路模板题,A*算法)

解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> #include<queue> using namespace std; typedef long long ll; const int N=1e3+10; const

POJ-1511 Invitation Cards( 最短路,spfa )

题目链接:http://poj.org/problem?id=1511 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 ha

POJ1511 Invitation Cards —— 最短路spfa

题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 29286   Accepted: 9788 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidine

poj1511||hdu1535 Invitation Cards spfa

题目链接: Invitation Cards 题意: 给出一个M个点N条边的单向图 1 <= M,N <= 1000000.   给出每条边的两端和经过所需要的时间,求从第一点分别到达所有其他点(2~M)再回到第一点的最短时间 题解: 1 <= M,N <= 1000000.     邻接矩阵肯定会爆内存  所以spfa邻接表解决即可 注意好反向边的建立 代码: #include<iostream> #include<cstdio> #include<

hdu1874 最短路模板题

之所以做了第二道模板题还要写是因为发现了一些自己的问题 用的是dij 最简单的松弛 需要注意的地方是松弛的时候 判断dis[i]<dis[w]+tance[w][i]时 还要再判断 vis[i] 要保证这个点没有成为过最小点 即这个点不会是已经被松弛过的点 输入的时候要注意 可能会有重边的输入 每次输入的时候进行一次判断 如果输入的是较大值 就不用更换了 关于memset的使用 它只能用来设置0与-1 别的值会出现莫名的错误 #include<stdio.h> #include<s

Til the Cows Come Home (最短路模板题)

个人心得:模板题,不过还是找到了很多问题,真的是头痛,为什么用dijkstra算法book[1]=1就错了..... 纠结中.... Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so s

HDU 1535 Invitation Cards (最短路,附SLF优化SPFA)

题目: http://acm.hdu.edu.cn/showproblem.php?pid=1535 题意: 有向图,求点1到点2-n的最短距离之和以及点2-n到点1的最短距离之和 方法: 1.跑1为原点的最短路 2.反向建图(把有向图的边反向,(u,v,w)变成(v,u,w)),跑1为原点的最短路 3.将两者距离之和加和即可(注意用 long long ,int会溢出) 1 void input() 2 { 3 scanf("%d%d", &n, &m); 4 g1.