POJ 3268 Silver Cow Party (Dijkstra)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13982 | Accepted: 6307 |
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 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
Sample Output
10 因为迪杰斯特拉求得是一点到其他所有点的最短路径,所以先把起点设成X,跑一次之后算出了每头牛回去的最短路径,然后把边反转,入边变成出边,出边变成入边,然后再把起点设成X,跑一次之后就算出了每头牛来的最短路径,最后把每头牛来回的最短路径加起来,取所有牛的最大值。写得太挫,明天补发其他解法。
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std; const int SIZE = ;
const int INF = 0x6fffffff;
int MAP[SIZE][SIZE];
bool VIS[SIZE][SIZE];
int BOX[SIZE];
int D[SIZE],S[SIZE];
int N,M,X;
struct Comp
{
bool operator ()(int & a,int & b)
{
return D[a] > D[b];
}
};
struct Node
{
int vec,cost;
};
vector<Node> G[SIZE]; void dijkstra(int);
int main(void)
{
Node temp;
int from; while(scanf("%d%d%d",&N,&M,&X) != EOF)
{
fill(&MAP[][],&MAP[SIZE - ][SIZE - ],INF);
fill(&VIS[][],&VIS[SIZE - ][SIZE - ],false);
fill(BOX,BOX + SIZE - ,);
for(int i = ;i <= N;i ++)
G[i].clear();
for(int i = ;i < M;i ++)
{
scanf("%d%d%d",&from,&temp.vec,&temp.cost);
G[from].push_back(temp);
MAP[from][temp.vec] = temp.cost;
}
dijkstra(X);
for(int i = ;i <= N;i ++)
BOX[i] = D[i]; for(int i = ;i <= N;i ++)
G[i].clear();
for(int i = ;i <= N;i ++)
for(int j = ;j <= N;j ++)
{
temp.vec = j;
temp.cost = MAP[j][i];
G[i].push_back(temp);
}
dijkstra(X);
int ans = -;
for(int i = ;i <= N;i ++)
{
BOX[i] += D[i];
ans = ans > BOX[i] ? ans : BOX[i];
}
printf("%d\n",ans);
} return ;
} void dijkstra(int s)
{
fill(D,D + SIZE,INF);
fill(S,S + SIZE,false);
priority_queue<int,vector<int>,Comp> que;
D[s] = ;
que.push(s); while(!que.empty())
{
int cur = que.top();
que.pop();
S[cur] = true; for(int i = ;i < G[cur].size();i ++)
if(!S[G[cur][i].vec] && D[G[cur][i].vec] > D[cur] + G[cur][i].cost)
{
D[G[cur][i].vec] = D[cur] + G[cur][i].cost;
que.push(G[cur][i].vec);
}
}
}
补发贝尔曼和SPFA
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std; const int INF = 0x6fffffff;
const int SIZE = ;
int N,M,X;
int MAP[SIZE][SIZE],D[SIZE],BOX[SIZE];
bool VIS[SIZE];
struct Node
{
int vec,cost;
};
vector<Node> G[SIZE]; void SPFA(int);
bool relax(int,int,int);
int main(void)
{
int from,to,cost,ans;
Node temp; while(scanf("%d%d%d",&N,&M,&X) != EOF)
{
fill(BOX,BOX + SIZE,);
fill(&MAP[][],&MAP[SIZE - ][SIZE - ],INF); for(int i = ;i < M;i ++)
{
scanf("%d%d%d",&from,&to,&cost);
MAP[from][to] = cost;
temp.vec = to;
temp.cost = cost;
G[from].push_back(temp);
}
SPFA(X);
for(int i = ;i <= N;i ++)
BOX[i] += D[i];
for(int i = ;i <= N;i ++)
{
G[i].clear();
for(int j = ;j <= N;j ++)
{
if(MAP[j][i] == INF)
continue;
temp.vec = j;
temp.cost = MAP[j][i];
G[i].push_back(temp);
}
}
SPFA(X);
ans = -;
for(int i = ;i <= N;i ++)
{
BOX[i] += D[i];
ans = ans > BOX[i] ? ans : BOX[i];
}
printf("%d\n",ans);
} return ;
} void SPFA(int s)
{
fill(D,D + SIZE,INF);
fill(VIS,VIS + SIZE,false);
D[s] = ;
queue<int> que;
que.push(s);
VIS[s] = true; while(!que.empty())
{
int cur = que.front();
que.pop();
VIS[cur] = false; for(int i = ;i < G[cur].size();i ++)
{
if(relax(cur,G[cur][i].vec,G[cur][i].cost))
{
que.push(G[cur][i].vec);
VIS[G[cur][i].vec] = true;
}
}
}
} bool relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
return true;
}
return false;
}
Bellman_ford
#include <iostream>
#include <cstdio>
using namespace std; const int INF = 0x6fffffff;
const int SIZE = ;
int D[SIZE],BOX[SIZE];;
int N,M,X;
struct Node
{
int from,to,cost;
}G[SIZE * ]; void Bellman_ford(int);
bool relax(int,int,int);
int main(void)
{
int ans; while(scanf("%d%d%d",&N,&M,&X) != EOF)
{
fill(BOX,BOX + SIZE,);
for(int i = ;i < M;i ++)
scanf("%d%d%d",&G[i].from,&G[i].to,&G[i].cost);
Bellman_ford(X);
for(int i = ;i <= N;i ++)
BOX[i] += D[i];
for(int i = ;i < M;i ++)
swap(G[i].from,G[i].to);
Bellman_ford(X);
ans = -;
for(int i = ;i <= N;i ++)
{
BOX[i] += D[i];
ans = ans > BOX[i] ? ans : BOX[i];
}
printf("%d\n",ans);
} return ;
} void Bellman_ford(int x)
{
fill(D,D + SIZE,INF);
D[x] = ; bool flag;
for(int i = ;i < N - ;i ++)
{
flag = false;
for(int j = ;j < M;j ++)
if(relax(G[j].from,G[j].to,G[j].cost))
flag = true;
if(!flag)
break;
}
} bool relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
return true;
}
return false;
}
POJ 3268 Silver Cow Party (Dijkstra)的更多相关文章
- POJ 3268 Silver Cow Party (Dijkstra + 优先队列)
题意:由n个牧场,编号1到n.每个牧场有一头牛.现在在牧场x举办party,每头牛都去参加,然后再回到自己的牧场.牧场之间会有一些单向的路.每头牛都会让自己往返的路程最短.问所有牛当中最长的往返路程是 ...
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 3268 Silver Cow Party (最短路dijkstra)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions:28457 Accepted: 12928 ...
随机推荐
- Java图片处理(一)图片合成
如何将多个头像合成类似QQ的群头像? 如上图所示,如何用java将单一的图片合成如上群头像. 在一个正方形外框中,要将多个图片合成上述图片.首先要做的是,依据圆相交的程度,计算圆心坐标与图片间空白区域 ...
- user is not in the sudoers file.This incident will be reported
我用普通用户ssk登陆,想让ssk成为拥有超级用户的权限的普通用户 开始提示输入密码错误 ,然后就这样了 解决方法如下: 1>.进入超级用户模式.也就是输入"su -", ...
- JavaScript一些不常用的写法
如何写JavaScript才能逼格更高呢?怎样才能组织JavaScript才能让别人一眼看出你不简单呢?是否很期待别人在看完你的代码之后感叹一句“原来还可以这样写”呢?下面列出一些在JavaScrip ...
- 用html5的canvas生成图片并保存到本地
原文:http://www.2cto.com/kf/201209/156169.html 前端的代码: [javascript] function drawArrow(angle) { ...
- js和jquery中的事件委托
[转+自己的修改] 概念: 什么是事件委托:通俗的讲,事件就是onclick,onmouseover,onmouseout,等就是事件,委托呢,就是让别人来做,这个事件本来是加在某些元素上的,然而你却 ...
- Java异常处理中finally中的return会覆盖catch语句中的return语句
Java异常处理中finally中的return会覆盖catch语句中的return语句和throw语句,所以Java不建议在finally中使用return语句 此外 finally中的throw语 ...
- win7和linux下的文件共享
在vmware虚拟机下安装linux系统,如果自个电脑的win7设置成自动获取IP的话,每次使用FTP文件传输服务器都要检查win7和linux系统的IP是否处于同一网段,如果不是还要手动设置.再有一 ...
- Netty4.x分析(转)
官网定义: netty是一个异步.事件驱动的网络应用框架,用于快速开发可维护的.高性能的服务端和客户端程序. 原理分析 Architecture Overview 网络模型:netty采用了React ...
- 【M33】将非尾端类设计为抽象类
1.考虑下面的需求,软件处理动物,Cat与Dog需要特殊处理,因此,设计Cat和Dog继承Animal.Animal有copy赋值(不是虚方法),Cat和Dog也有copy赋值.考虑下面的情况: Ca ...
- 关于java.lang.IllegalArgumentException: View not attached to window manager 错误的分析
今天遇到一个很奇特的问题,当用户设置了PIN码,在锁屏界面正常解锁PIN码后,进入Launcher时显示com.android.phone 已停止运行.一开始猜想会不会是解锁PIN码的时候处理导致了P ...