最短路练习10/poj/1511 Invitation Cards ,(两次spfa),(单源最短路,优先队列优化的Dijkstra)
来源:互联网 发布:淘宝乐高军事玩具大全 编辑:程序博客网 时间:2024/06/10 03:32
题目链接:http://poj.org/problem?id=1511
Invitation Cards
Time Limit: 8000MS Memory Limit: 262144KTotal Submissions: 27231 Accepted: 9034
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.
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
22 21 2 132 1 334 61 2 102 1 601 3 203 4 102 4 54 1 50
Sample Output
46210题意:现在有n个点,m条单向边;现在求每个点走到点1,在返回到原来的位置的最短路距离之和;(意思就是每个点都要去一趟点1,在返回,求所有点的所走距离之和的最小值)
思路:从点1到每个点的距离最小值能用spfa求出来;那么反过来每个点到点1的距离也能求出来;
我自己写的spfa方法:AC代码:
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<map>#include<string>#define LL long long#define eps 1e-8using namespace std;const int mod = 1e7+7;const LL inf = 0x3f;const int maxx = 1e6 +10;int t,n,m;struct node{ int x,y,next,nexx; LL num;}point[maxx];int head[maxx];int head2[maxx];//head[i]表示i能到的地方:与next对应;head2[i]表示能到i的地方:与nexx对应;LL dis[maxx];int vis[maxx];LL spfa1(){ memset(vis,0,sizeof(vis)); memset(dis,inf,sizeof(dis));//dis[i]表示1到i的最小距离; queue<int>q; q.push(1); vis[1]=1; dis[1]=0; while(!q.empty()) { int f=q.front(); q.pop(); vis[f]=0; for(int i=head[f];i;i=point[i].next) { if(dis[point[i].x]+point[i].num<dis[point[i].y]) { dis[point[i].y]=dis[point[i].x]+point[i].num; if(!vis[point[i].y]) { vis[point[i].y]=1; q.push(point[i].y); } } } } LL ans=0; for(int i=1;i<=n;i++) ans+=dis[i]; return ans;}LL spfa2(){ memset(vis,0,sizeof(vis)); memset(dis,inf,sizeof(dis));//dis[i]表示i到1的最小距离 queue<int>q; q.push(1); vis[1]=1; dis[1]=0; while(!q.empty()) { int f=q.front(); q.pop(); vis[f]=0; for(int i=head2[f];i;i=point[i].nexx) { if(dis[point[i].y]+point[i].num<dis[point[i].x]) { dis[point[i].x]=dis[point[i].y]+point[i].num; if(!vis[point[i].x]) { vis[point[i].x]=1; q.push(point[i].x); } } } } LL ans=0; for(int i=1;i<=n;i++) ans+=dis[i]; return ans;}int main(){ scanf("%d",&t); while(t--) { memset(head,0,sizeof(head)); memset(head2,0,sizeof(head2)); scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { scanf("%d%d%lld",&point[i].x,&point[i].y,&point[i].num); point[i].next=head[point[i].x];//spfa1的 head[point[i].x]=i; point[i].nexx=head2[point[i].y];//spfa2的,邻接表用head数组表示 head2[point[i].y]=i; } printf("%lld\n",spfa1()+spfa2()); }}
kuangbin大神的代码:(单源最短路,优先队列优化的Dijkstra)
#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#include <queue>#include <vector>using namespace std;/* * 使用优先队列优化Dijkstra算法 * 复杂度O(ElogE) * 注意对vector<Edge>E[MAXN]进行初始化后加边 */const int INF=0x3f3f3f3f;const int MAXN=1000010;struct qnode{ int v; int c; qnode(int _v=0,int _c=0):v(_v),c(_c){} bool operator <(const qnode &r)const { return c>r.c; }};struct Edge{ int v,cost; Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}};vector<Edge>E[MAXN];bool vis[MAXN];int dist[MAXN];void Dijkstra(int n,int start)//点的编号从1开始{ memset(vis,false,sizeof(vis)); for(int i=1;i<=n;i++)dist[i]=INF; priority_queue<qnode>que; while(!que.empty())que.pop(); dist[start]=0; que.push(qnode(start,0)); qnode tmp; while(!que.empty()) { tmp=que.top(); que.pop(); int u=tmp.v; if(vis[u])continue; vis[u]=true; for(int i=0;i<E[u].size();i++) { int v=E[tmp.v][i].v; int cost=E[u][i].cost; if(!vis[v]&&dist[v]>dist[u]+cost) { dist[v]=dist[u]+cost; que.push(qnode(v,dist[v])); } } }}void addedge(int u,int v,int w){ E[u].push_back(Edge(v,w));}int A[MAXN],B[MAXN],C[MAXN];int main(){// freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout); int n,m; int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=0;i<m;i++) scanf("%d%d%d",&A[i],&B[i],&C[i]); for(int i=1;i<=n;i++)E[i].clear(); for(int i=0;i<m;i++)addedge(A[i],B[i],C[i]); Dijkstra(n,1); long long ans=0; for(int i=1;i<=n;i++)ans+=dist[i]; for(int i=1;i<=n;i++)E[i].clear(); for(int i=0;i<m;i++)addedge(B[i],A[i],C[i]); Dijkstra(n,1); for(int i=1;i<=n;i++)ans+=dist[i]; printf("%I64d\n",ans); } return 0;}
0 0
- 最短路练习10/poj/1511 Invitation Cards ,(两次spfa),(单源最短路,优先队列优化的Dijkstra)
- POJ 1511 Invitation Cards 【最短路,spfa算法,Dijkstra算法堆优化】
- poj 1511 Invitation Cards(spfa最短路)
- POJ 1511 Invitation Cards 最短路SPFA
- POJ 1511 Invitation Cards 求来回最短路(spfa)
- poj 1511&&zoj 2008 Invitation Cards 最短路+SPFA
- POJ 1511 Invitation Cards(spfa最短路)
- POJ 1511Invitation Cards SPFA最短路问题
- POJ 1511 Invitation Cards【来回最短路+SPFA】
- poj 1511 Invitation Cards 最短路SPFA(模板题)
- POJ 1511 Invitation Cards(最短路spfa算法)
- POJ 1511 Invitation Cards(最短路spfa算法)
- TOJ 2217. Invitation Cards【最短路dijkstra+堆优化或者SPFA】
- TOJ 2217. Invitation Cards【最短路dijkstra+堆优化或者SPFA】
- 【原创】最短路模板 Floyd,优先队列优化dijkstra,SPFA
- POJ-1511 Invitation Cards【最短路】
- POJ 1511 Invitation Cards (最短路)
- poj 1511 Invitation Cards(最短路)
- AnimationCurve
- Oracle把逗号分割的字符串转换为可放入in的条件语句的字符数列
- DBI(i80)/DPI(RGB)/DSI
- java学习 十五、面向对象
- Extjs监听浏览器刷新、关闭和窗口关闭功能
- 最短路练习10/poj/1511 Invitation Cards ,(两次spfa),(单源最短路,优先队列优化的Dijkstra)
- Unity脚本引用原理,修复Unity脚本引用丢失,源码脚本与dll中的脚本引用互换 .
- StreamTool 字节流保存,字节流读取下一行,读取流
- grep
- svn certificate validation failed解决办法
- 最长公共子序列问题·
- 跳表实现
- 1758: [Wc2010]重建计划(TLE)
- Unity3D 自动添加Fbx Animation Event