Til the Cows Come Home
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.
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
* Line 1: Two integers: T and N
* 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
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
Hint
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
此题是最短路的模板题,最短路的几种方法均可以使用,可以用来熟悉最短路的模板
模板一:dijkstra算法
ps:适用于稠密图,通过图中的点来计算,但无法解决负边权的问题,算法复杂度:O(n^2)
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int dist[maxn];
bool vis[maxn];
int mapp[1200][1200];
int n,m;
void dijkstra(int s)
{
dist[s]=0;
rep(i,1,n) {
int t=inf;
int nape=-1;
rep(j,1,n) {
if(!vis[j]&&dist[j]<t)
{
t=dist[j];
nape=j;
}
}
if(nape==-1)
break;
vis[nape]=true;
rep(j,1,n) {
if(dist[nape]+mapp[nape][j]<dist[j])
dist[j]=dist[nape]+mapp[nape][j];
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ios::sync_with_stdio(0),cin.tie(0);
cin>>m>>n;
rep(i,1,n) {
rep(j,1,n) {
mapp[i][j]=inf;
}
vis[i]=false;
dist[i]=inf;
mapp[i][i]=0;
}
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
if(c<mapp[a][b])
{
mapp[a][b]=c;
mapp[b][a]=c;
}
}
dijkstra(n);
if(dist[1]!=inf)
cout<<dist[1]<<endl;
else
cout<<"-1"<<endl;
return 0;
}
模板二:dijkstra优化(堆优化)
同样适用于稠密图,算法复杂度为O(n*logn)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e4 + 5;
const ll mod = 1e9+7;
bool vis[maxn];
struct node
{
int to;
int val;
node(int _to=0,int _val=0):to(_to),val(_val){}
bool operator < (const node &x)const {
return val>x.val;
}
};
struct edge
{
int to;
int val;
edge(int _to=0,int _val=0):to(_to),val(_val){}
};
vector<edge>e[maxn];
int dist[maxn];
int n,m;
void dijkstra(int s)
{
dist[s]=0;
priority_queue<node>p;
while(!p.empty())
{
p.pop();
}
node tmp;
p.push(node(s,0));
while(!p.empty())
{
tmp=p.top();
p.pop();
int u=tmp.to;
if(vis[u])
continue;
vis[u]=true;
for(int i=0;i<e[u].size();i++)
{
int to=e[u][i].to;
int val=e[u][i].val;
if(!vis[to]&&dist[u]+val<dist[to])
{
dist[to]=dist[u]+val;
p.push(node(to,dist[to]));
}
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ios::sync_with_stdio(0),cin.tie(0);
cin>>m>>n;
fill(dist+1,dist+1+n,inf);
memset(vis,false,sizeof(vis));
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
e[a].push_back(edge(b,c));
e[b].push_back(edge(a,c));
}
dijkstra(n);
if(dist[1]!=inf)
cout<<dist[1]<<endl;
else
cout<<"-1"<<endl;
return 0;
}
模板三:bellman_ford算法
适用于稀疏图,可以判断是否存在负边权,算法复杂度为O(NE)ps:N为点数,E为边数
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int dist[maxn];
struct edge
{
int u;
int v;
int val;
edge(int _u=0,int _v=0,int _val=0):u(_u),v(_v),val(_val){}
};
bool vis[maxn];
vector<edge>e;
int m,n;
bool bellman_ford(int s)
{
dist[s]=0;
rep(i,1,n) {
bool f=false;
for(int j=0;j<e.size();j++)
{
int u=e[j].u;
int v=e[j].v;
int val=e[j].val;
if(dist[u]+val<dist[v])
{
dist[v]=dist[u]+val;
f=true;
}
}
if(!f)
return true;
}
for(int j=0;j<e.size();j++) {
if(dist[e[j].u]+e[j].val<dist[e[j].v])
return false;
}
return true;
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ios::sync_with_stdio(0),cin.tie(0);
cin>>m>>n;
fill(dist+1,dist+1+n,inf);
memset(vis,false,sizeof(vis));
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
e.push_back(edge(a,b,c));
e.push_back(edge(b,a,c));
}
bool t=bellman_ford(n);
if(t&&dist[1]!=inf)
cout<<dist[1]<<endl;
else
cout<<"-1"<<endl;
return 0;
}
模板四:safa算法
适用于稀疏图,算法复杂度为O(kE)PS:k为常数,E为边数
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
struct edge
{
int to;
int val;
edge(int _to=0,int _val=0):to(_to),val(_val){}
};
vector<edge>e[maxn];
int dist[maxn];
bool vis[maxn];
int cnt[maxn];
int m,n;
bool spfa(int s)
{
vis[s]=true;
dist[s]=0;
queue<int >q;
while(!q.empty())
q.pop();
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=0;i<e[u].size();i++)
{
int v=e[u][i].to;
int val=e[u][i].val;
if(dist[u]+val<dist[v])
{
dist[v]=dist[u]+val;
if(!vis[v])
{
vis[v]=true;
q.push(v);
if(++cnt[v]>n)
return false;
}
}
}
}
return true;
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ios::sync_with_stdio(0),cin.tie(0);
cin>>m>>n;
fill(dist+1,dist+1+n,inf);
memset(vis,false,sizeof(vis));
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
e[a].push_back(edge(b,c));
e[b].push_back(edge(a,c));
}
bool t=spfa(n);
if(t&&dist[1]!=inf)
cout<<dist[1]<<endl;
else
cout<<"-1"<<endl;
return 0;
}