Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 33015 | Accepted: 11174 |
Description
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
Output
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90 理论上的效率应该是Dijsktra > SPFA > Bellman_Ford,但是前两者我用了vector,影响了效率,导致贝尔曼是最快的,迪杰斯特拉其次。
#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
using namespace std; const int SIZE = ;
const int INF = 0x2fffffff;
bool S[SIZE];
int N,D[SIZE];
struct Node
{
int vec,cost;
};
struct comp
{
bool operator ()(int & a,int & b)
{
return D[a] > D[b];
}
};
vector<Node> G[SIZE];
priority_queue <int,vector<int>,comp> QUE; void dijkstra(int);
void relax(int,int,int);
int main(void)
{
int t,from;
Node temp; scanf("%d%d",&t,&N);
while(t --)
{
scanf("%d%d%d",&from,&temp.vec,&temp.cost);
G[from].push_back(temp);
swap(from,temp.vec);
G[from].push_back(temp);
}
dijkstra(N);
printf("%d\n",D[]); return ;
} void dijkstra(int s)
{
fill(D,D + SIZE,INF);
D[s] = ;
S[s] = true;
QUE.push(s); while(!QUE.empty())
{
int cur = QUE.top();
int len = G[cur].size();
S[cur] = true;
QUE.pop();
for(int i = ;i < len;i ++)
relax(cur,G[cur][i].vec,G[cur][i].cost);
if(cur == )
return ;
}
} void relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
if(!S[to])
QUE.push(to);
}
}
Dijkstra
#include <iostream>
#include <cstdio>
using namespace std; const int INF = 0x5fffffff;
const int SIZE = ;
bool UPDATE;
int D[SIZE];
int N,E;
struct Node
{
int from,to,cost;
}Edge[SIZE * ]; void Bellman_Ford(int);
void relax(int,int,int);
int main(void)
{
int t;
Node temp; scanf("%d%d",&t,&N);
while(t --)
{
scanf("%d%d%d",&temp.from,&temp.to,&temp.cost);
Edge[E ++] = temp;
swap(temp.from,temp.to);
Edge[E ++] = temp;
}
Bellman_Ford(N);
printf("%d\n",D[]); return ;
} void Bellman_Ford(int s)
{
fill(D,D + SIZE,INF);
D[s] = ; for(int i = ;i < N - ;i ++)
{
UPDATE = false;
for(int j = ;j < E;j ++)
relax(Edge[j].from,Edge[j].to,Edge[j].cost);
if(!UPDATE)
return ;
}
} void relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
UPDATE = true;
}
}
Bellman-Ford
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std; const int SIZE = ;
const int INF = 0x5fffffff;
int N,D[SIZE];
bool IN_QUE[SIZE];
struct Node
{
int to,cost;
};
vector<Node> G[SIZE]; void spfa(int);
bool relax(int,int,int);
int main(void)
{
int t,from;
Node temp; scanf("%d%d",&t,&N);
while(t --)
{
scanf("%d%d%d",&from,&temp.to,&temp.cost);
G[from].push_back(temp);
swap(from,temp.to);
G[from].push_back(temp);
}
spfa(N);
printf("%d\n",D[]); return ;
} void spfa(int s)
{
int vec,cost;
queue<int> que;
fill(D,D + SIZE,INF);
D[s] = ;
IN_QUE[s] = true;
que.push(s); while(!que.empty())
{
int cur = que.front();
int len = G[cur].size();
IN_QUE[cur] = false;
que.pop(); for(int i = ;i < len;i ++)
{
vec = G[cur][i].to;
cost = G[cur][i].cost;
if(relax(cur,vec,cost) && !IN_QUE[vec])
{
IN_QUE[vec] = true;
que.push(vec);
}
}
}
} bool relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
return true;
}
return false;
}
SPFA