标签:
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
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 }
poj1511/zoj2008 Invitation Cards(最短路模板题)
标签:
原文地址:http://www.cnblogs.com/fraud/p/4304363.html