poj2387~Til the Cows Come Home(最短路dijkstra)

来源:互联网 发布:网络卡盟 编辑:程序博客网 时间:2024/05/22 13:28
Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 48638 Accepted: 16537

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 51 2 202 3 303 4 204 5 201 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.

Source

USACO 2004 November

一个简单的单源最短路问题,本来想直接用floyd写,超时,果然不能懒。改用dijkstra,首先贴一个邻接矩阵写的

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3fusing namespace std;int e[2005][2005];int dis[2005];int book[2005];int n,m;void dijkstra(){    mem(book,0);    book[1]=1;    for(int i=1;i<=m;i++)        dis[i]=e[1][i];    for(int i=1;i<m;i++)    {        int minn=inf;        int u;        for(int j=1;j<=m;j++)        {            if(book[j]==0&&dis[j]<minn)            {                minn=dis[j];                u=j;            }        }        book[u]=1;        for(int v=1;v<=m;v++)        {            if(e[u][v]<inf&&dis[v]>dis[u]+e[u][v])                dis[v]=dis[u]+e[u][v];        }    }}int main(){    int i,j;       scanf("%d%d",&n,&m);            for(i=0;i<=m;i++)          {        for(j=0;j<=m;j++)        {            if(i==j)e[i][j]=0;            else e[i][j]=inf;        }           }           //图的初始化,最短路一般是初始化无穷大,最长路初始化无穷小   while(n--)    {        int a,b,c;        scanf("%d%d%d",&a,&b,&c);        if(c<e[a][b])        e[a][b]=e[b][a]=c;//此处小坑,如果此点输入了几次,存较小的    }    dijkstra();    printf("%d\n",dis[m]);}
临界矩阵79ms,再用邻接表写一下,正好练练邻接表,不会指针,只好学数组模拟的了

#include<stdio.h>#include<string.h>#include<stack>#include<stdlib.h>#include<queue>#include<math.h>#include<algorithm>#include<vector>#define mem(a,b) memset(a,b,sizeof(a))#define inf 999999999using namespace std;const int maxn=4010;int u[maxn],v[maxn],w[maxn],first[maxn],next[maxn],book[maxn],dis[maxn];int t,n;void dijkstra(){    mem(book,0);    for(int i=1;i<=n;i++)        dis[i]=inf;    dis[1]=0;    book[1]=1;    int minn,k=1;    for(int i=2;i<=n;i++)    {        for(int j=first[k];j!=-1;j=next[j])//邻接表的遍历        {            if(book[v[j]]==0&&dis[v[j]]>dis[k]+w[j])                dis[v[j]]=dis[k]+w[j];        }        minn=inf;        for(int j=1;j<=n;j++)        {            if(book[j]==0&&minn>dis[j])            {                k=j;                minn=dis[j];            }        }        book[k]=1;    }}int main(){    while(~scanf("%d%d",&t,&n))        {            mem(first,-1);            //建立邻接表,由于是双向图,所以有个小技巧            for(int i=1;i<=t*2;i+=2)            {                int a,b,c;                scanf("%d%d%d",&a,&b,&c);                u[i]=v[i+1]=a;                v[i]=u[i+1]=b;                w[i]=w[i+1]=c;                next[i]=first[u[i]],next[i+1]=first[u[i+1]];                first[u[i]]=i,first[u[i+1]]=i+1;            }            dijkstra();            printf("%d\n",dis[n]);        }     return 0;}
邻接表跑了47ms还算优化了一些


0 0
原创粉丝点击