您的位置:首页 > 其它

[kuangbin带你飞]专题四 最短路练习D

2016-12-03 10:50 323 查看
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?

http://poj.org/problem?id=3268

题意:

给定一个有向图(可带环),然后要求所有点到特定点e的距离,然后规定每个

点到e之后,还要返回原地,注意图是有向图,去跟回来的路径可能不同。

问来回时间最长的那个点来回共需要多长时间

##tip:

做一次dijstra,把图反过来再做一次。。。

你特么说好了1000个点。。。开1010个点过不去。。10010就过了。。。浪费一个小时。。。

#include <cstdio>
#include<iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
using namespace std;
const int maxn = 10100;
const int maxm = 200010;
int n,m,x,tot,first[maxn],u[maxn],v[maxn],w[maxn];
int dis[2][maxn];
typedef pair<int,int>pii;
priority_queue<pii,vector<pii>,greater<pii> >pq;
const int INF = (1<<30);

struct node{
int v,next,w;
}edges[maxm];
void add(int u,int v,int w){
edges[tot].v = v;edges[tot].w = w;edges[tot].next = first[u];first[u] = tot++;
}

void dij(int p){
while(!pq.empty()){
int no = pq.top().second;
pq.pop();
for(int k = first[no];k != -1 ; k = edges[k].next){
if(dis[p][edges[k].v] > dis[p][no]+edges[k].w){
dis[p][edges[k].v] = dis[p][no]+edges[k].w;
pq.push(make_pair(dis[p][edges[k].v],edges[k].v));
}
}
}
}

void init(){
memset(first,-1,sizeof(first));
while(!pq.empty())  pq.pop();
tot = 0;
for(int i = 1; i <= n ; i++)    dis[0][i] = INF;
for(int i = 0 ; i < m ; i++){
scanf("%d%d%d",&u[i],&v[i],&w[i]);
add(u[i],v[i],w[i]);
}
pq.push(make_pair(0,x));
dis[0][x] = 0;
dij(0);
}

void ini(){
memset(first,-1,sizeof(first));
while(!pq.empty())  pq.pop();
tot = 0;
for(int i = 1; i <= n ; i++)    dis[1][i] = INF;
for(int i = 0 ; i < m ; i++)
add(v[i],u[i],w[i]);
pq.push(make_pair(0,x));
dis[1][x] = 0;
dij(1);
}

void print(){
int maxm = 0;
for(int i = 1; i <= n ; i++){
int ans = dis[0][i]+dis[1][i];
// printf("dis[0][%d] = %d , dis[1][%d] = %d\n",i,dis[0][i],i,dis[1][i]);
maxm = max(maxm,ans);
}
printf("%d\n",maxm);
}

int main(){
while(~scanf("%d%d%d",&n,&m,&x)){
init();
ini();
print();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: