Til the Cows Come Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37662   Accepted: 12836

Description

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
3 4 20
4 5 20
2 3 30 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.

SPFA:

 #include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm> using namespace std;
const int INF = ;
const int MAX = + ;
int t,n;
struct point
{
int e,w;
};
vector<point> g[MAX];
int dist[MAX];
void spfa(int v)
{
for(int i = ; i <= n; i++)
{
dist[i] = INF;
}
dist[v] = ;
queue<int> que;
que.push(v);
while(que.size())
{
int x = que.front();
que.pop();
int len = g[x].size();
for(int i = ; i < len; i++)
{
int y = g[x][i].e;
if(dist[y] > dist[x] + g[x][i].w)
{
dist[y] = dist[x] + g[x][i].w;
que.push(y);
}
}
}
}
int main()
{
while(scanf("%d%d", &t, &n) != EOF)
{
for(int i = ; i < MAX; i++)
g[i].clear(); while(t--)
{
int s,e,w;
point temp;
scanf("%d%d%d", &s,&e,&w);
temp.w = w;
temp.e = e;
g[s].push_back(temp);
temp.e = s;
g[e].push_back(temp);
} spfa(n);
printf("%d\n",dist[]);
} return ;
}

SPFA

Dijkstra

注意重边问题

 #include <cstring>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
const int INF = ;
const int MAX = + ;
int g[MAX][MAX],dist[MAX],vis[MAX];
int t,n;
void Dijkstra()
{
for(int i = ; i <= n; i++)
dist[i] = INF;
memset(vis,,sizeof(vis));
dist[n] = ;
vis[n] = ;
int pos = n;
for(int i = ; i < n; i++)
{
int minn = INF;
for(int j = ; j <= n; j++)
{
if(vis[j] == && dist[j] < minn)
{
minn = dist[j];
pos = j;
}
}
vis[pos] = ;
for(int j = ; j <= n; j ++)
{
if(vis[j] == && dist[j] > dist[pos] + g[pos][j])
dist[j] = dist[pos] + g[pos][j];
}
}
}
int main()
{
while(scanf("%d%d",&t,&n) != EOF)
{
int s,e,w;
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
g[i][j] = INF;
}
}
for(int i = ; i < t; i++)
{
scanf("%d%d%d",&s,&e,&w);
if(g[s][e] > w)
g[s][e] = g[e][s] = w;
}
Dijkstra();
printf("%d\n",dist[]);
} }

Ballem_ford

 #include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int INF = ;
const int MAX = + ;
int n,t;
struct point
{
int s,t,w;
};
vector<point> g;
int dist[MAX];
void Ballem_ford(int v)
{
for(int i = ; i <= n; i++)
dist[i] = INF;
dist[v] = ;
for(int j = ; j < n; j++)
{
int len = g.size();
int flag = ;
for(int i = ; i < len; i++)
{
int s = g[i].s;
int t = g[i].t;
int w = g[i].w;
if(dist[t] > dist[s] + w)
{
dist[t] = dist[s] + w;
flag = ;
}
}
if(flag == ) //加个flag 优化一下
break;
}
}
int main()
{
while(scanf("%d%d", &t, &n) != EOF)
{
g.clear();
int s,e,w;
point temp;
for(int i = ; i < t; i++)
{
scanf("%d%d%d", &s,&e,&w);
temp.w = w;
temp.t = e;
temp.s = s;
g.push_back(temp);
temp.t = s;
temp.s = e;
g.push_back(temp);
}
Ballem_ford(n);
printf("%d\n",dist[]);
}
}

最新文章

  1. Windows phone应用开发[22]-再谈下拉刷新
  2. please wait while windows configures microsoft visual studio professional 2013 [转载]
  3. js判断浏览器
  4. 洛谷P2246 SAC#1 - Hello World(升级版)
  5. JQuery选择器细节-遁地龙卷风
  6. 转载:GridControl使用技巧
  7. Leetcode: Lexicographical Numbers
  8. coins_多重背包
  9. CUDA学习笔记(一)【转】
  10. win7 mount到Linux下无法分配内存的问题(Cannot allocate memory)
  11. cf 383 D
  12. 剑指Offer22 判断数组是否为某二叉搜索树的后序遍历
  13. 多线程程序设计学习(12)Thread-soecific storage pattern
  14. 记一次T-SQL查询优化 索引的重要性
  15. \r \r\n \t 的区别
  16. Java开发工具IntelliJ IDEA单元测试和代码覆盖率图解
  17. Hadoop入门进阶步步高(六)-Hadoop1.x与Hadoop2的差别
  18. B进制加法(洛谷1604)
  19. 使用.Net Core+EF7 CodeFirst(2)
  20. js获取对象长度和名称

热门文章

  1. openstack常规操作命令梳理
  2. isScroll代码
  3. c语言 指针的值
  4. no.1
  5. php基础07:流程控制
  6. C# MD5加密解密帮助类
  7. pandas groupby
  8. 利用php实现文件迁移重命名
  9. html的转码玉反转码
  10. ionic 项目笔记