poj 3268 Silver Cow Party Dijkstra 和SPFA
来源:互联网 发布:淘宝达人大v认证难不难 编辑:程序博客网 时间:2024/04/29 19:06
一开始做这个题目,很快写完,结果超时,我傻了,居然是n的3次方。。后来无语教我简化了,过了。。。
题目大意就是给出N个点和M条边,每两个点之间是单行道,来回路长不一定相等,给定一个确定的点,算所有点中去这个确定的点再回去的最短路径中的最大值。
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Sample Input
4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3
Sample Output
10
还是用Dijkstra,加上他的转置就可以了。。
代码:
#include <stdio.h>#include <iostream>#include <algorithm>#include <stdlib.h>#include <string.h>#define INF 99999999;using namespace std;int vis[1001];int map1[1001][1001],map2[1001][1001];int d1[1001],d2[1001],n;void Dijkstra1 (int s,int d[],int map[][1001]){ int i,j,m,p; memset(vis,0,sizeof(vis)); for(i=0;i<n;i++) { d[i]=map[s][i]; } for(i=0;i<n;i++) { m=INF; p=-1; for(j=0;j<n;j++) { if(vis[j]==0 && d[j]<m) { m=d[j]; p=j; } } if(p==-1) break; vis[p]=1; for(j=0;j<n;j++) { if(vis[j]==0 && d[j]>map[p][j]+d[p]) { d[j]=map[p][j]+d[p]; } } }}int main(){ int m,i,j,k,q,a,b,t,max; while(scanf("%d%d%d",&n,&m,&q)!=EOF) { for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i==j){map1[i][j]=0;map2[j][i]=0;} else{map1[i][j]=INF; map2[j][i]=INF;} } } for(i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&t); a--; b--;map1[a][b]=min(t,map1[a][b]);map2[b][a]=min(t,map2[b][a]); //map2 为map1的转置。 } Dijkstra1(q-1,d1,map1); Dijkstra1(q-1,d2,map2);max=0;for(i=0;i<n;i++){if(max<d1[i]+d2[i])max=d1[i]+d2[i];//printf("...%d...%d..\n",d1[i],d2[i]);} printf("%d\n",max); }}
中午的时候又写了一下这道题目的spfa算法 ,还挺简单,无语说这个速度跟快,尤其是在边少的时候。。
代码:
#include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>#include <queue>#define INF 999999999using namespace std;int vis[1001];int d1[1001],d2[1001];int map1[1001][1001],map2[1001][1001],n;void spfa(int s,int d[],int map[][1001]) //出发点s。{ memset(vis,0,sizeof(vis)); queue<int> q; int i,j,m,p; for(i=0;i<n;i++) //一开始付初值,这个我原来写的是Dijkstra那个付初值,后来老错。 { d[i]=INF; }while(!q.empty()) //清空队列q.pop(); q.push(s); //初始化d[s]=0; while(!q.empty()) { int now=q.front(); q.pop();vis[now]=0; for(i=0;i<n;i++) { if(d[i]>map[now][i]+d[now]) //路径松弛。 { d[i]=map[now][i]+d[now]; if(vis[i]==0) //判断是否在队列中,不在,压入队列。 { q.push(i); vis[i]=1; } } } } return ;}int main(){ int m,i,j,a,b,t,q; while(scanf("%d%d%d",&n,&m,&q)!=EOF) { for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i==j) { map1[i][j]=0; map2[j][i]=0; } else { map1[i][j]=INF; map2[i][j]=INF; } } } for(i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&t); a--; b--; map1[a][b]=__min(t,map1[a][b]); map2[b][a]=__min(t,map2[b][a]); } spfa(q-1,d1,map1); spfa(q-1,d2,map2); int max=0; for(i=0;i<n;i++) { if(max<d1[i]+d2[i]) { max=d1[i]+d2[i]; }// printf("....%d....%d..\n",d1[i],d2[i]); } printf("%d\n",max); } return 0;}
- poj 3268 Silver Cow Party , spfa , dijkstra
- poj 3268 Silver Cow Party Dijkstra 和SPFA
- poj 3268 Silver Cow Party(dijkstra||SPFA)(中等)
- Silver Cow Party poj 3268 Dijkstra,spfa,+vector
- SPFA POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party(SPFA)
- poj 3268 Silver Cow Party (spfa )
- Silver Cow Party - POJ 3268 spfa
- poj 3268 Silver Cow Party(SPFA)
- Poj 3268 Silver Cow Party【SPFA】
- POJ 3268 Silver Cow Party(Spfa)
- POJ 3268 Silver Cow Party(Dijkstra算法)
- POJ 3268 - Silver Cow Party(dijkstra)
- POJ 3268 Silver Cow Party (Dijkstra~)
- POJ 3268 Silver Cow Party(dijkstra)
- POJ 3268 Silver Cow Party(Dijkstra)
- Dijkstra-POJ-3268-Silver Cow Party
- POJ 3268Silver Cow Party dijkstra();
- 关于windows句柄机制的一些心得体会
- 关于InvocationHandler接口
- tomcat6 The duplicate definition has been ignored
- ConcurrentHashMap(转载)
- 开源日志系统比较
- poj 3268 Silver Cow Party Dijkstra 和SPFA
- COACH: luxury alternative route
- ctype.h简介
- Oracle异常 ORA-04068: ORA-04061: ORA-04065: ORA-06508 解决办法 (一)
- 人生苦如酒 只有浅斟慢酌才有味道
- pl/sql控制结构(二)
- 如何使用Python发送带(附件)的邮件
- POJ--1191[棋盘分割] 经典的记忆化搜索
- 开发工作中常用的linux命令(持续更新)