HDU 2112 Today dijsktra||floyed

                                      HDU Today

With the help of the kits, Haidong Group finally got through the crisis. Since then, the development of HDU has been smooth sailing. By 2050, the group has become quite large, and it is said that it has entered the top 500 of Qianjiang Shredded Pork Economic Development Zone. At this time, the XHD couple also retreated to the second line, and bought a house in Taoyao Village, Weipu Town, Zhuji City, with beautiful scenery, and began to spend their old age in peace.  
After living like this for a while, Mr. Xu still doesn't know much about the local traffic. Sometimes I feel very depressed. I want to go to a place, but I don’t know what bus to take, where to transfer, and where to get off (in fact, Mr. Xu owns a car, but he must have fun with the people. This is Mr. Xu’s character) .  
Mr. Xu often asks directions in broken English: "Can you help me?". Looking at his confused and helpless eyes, can you help him enthusiastically?  
Please help him reach his destination in the shortest time possible (assuming that every bus stops only at the starting and ending stops, and will leave at any time).  
Input There are multiple groups of input data, the first line of each group is the total number of buses N (0<=N<=10000);  
the second line has Xu's location start, his destination end;  
then there are n lines, Each line has the station name s, the station name e, and the time integer t (0<t<100) from s to e (each place name is a string with a length not exceeding 30).  
note: The number of place names in a set of data will not exceed 150.  
If N==-1, it means the end of input.  
Output If Xu can always reach the destination, output the shortest time; otherwise, output "-1".  
Sample Input
6
xiasha westlake
xiasha station 60
xiasha ShoppingCenterofHangZhou 30
station westlake 20
ShoppingCenterofHangZhou supermarket 10
xiasha supermarket 50
supermarket westlake 10
-1
Sample Output
50


Hint:
The best route is:
xiasha->ShoppingCenterofHangZhou->supermarket->westlake


Although occasionally get lost, but because of your help
** and ** have lived happily ever after.

--The end--

         The topic is very interesting, and it is also a relatively watery topic. I started to write it with dijsktra, and then I wanted to try it with floyed, but there was no TLE.
       dijsktra is written in adjacency matrix without using priority queue optimization.
        For this topic, we only need to reprocess the information and convert the characters into numbers. Obviously, we will think of using map mapping. It's the first time I've learned STL for so long
        With map hahahahaha ~~~ After the New Year, I wish you all a happy year, I hope I can work harder next year.
Let's take a look at the code:
               The code has written a little comment that I understand
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#include<list>
#include<map>
using namespace std;
const int INF = 1e9;//写floyed时候 最大值设成 (unsigned int)(-1)>>1; WA了  太不细心了 这里设置1e9理论最大值就好了
const int maxn = 1005;
int m[maxn][maxn],dis[maxn],vis[maxn];
void init()
{
    /*dij 初始化
    for(int i=0; i<maxn; i++)
    {
        dis[i]=INF;
        vis[i]=0;
        for(int j=0; j<maxn; j++)
            m[i][j]=INF;
    }*/
    /*floyed 初始化*/
    for(int i=0; i<maxn; i++)
    {
        for(int j=0; j<maxn; j++)
        {
            if(i==j)
                m[i][j]=0;
            else
            {
                m[i][j]=INF;
            }
        }
    }
}
/*void dijsktra(int start, int n)
{
    for(int i=1; i<=n; i++)
        dis[i]=m[start][i];
    dis[start] = 0;
    vis[start] = 1 ;
    for(int i=0; i<n; i++)
    {
        int mm=INF,v;
        for(int j=0; j<n; j++)
            if(!vis[j]&&dis[j]<mm)
                mm=dis[j],v=j;
        if(mm==INF) break;
        vis[v]=1;
        for(int j=0; j<n; j++)
            if(m[v][j]<INF&&!vis[j])
                dis[j]=min(dis[j],dis[v]+m[v][j]);
    }
}*/
void floyed(int d)//  相对比dij floyed还是很简单些的
{

    for(int k=1; k<=d; k++)
        for(int i=1; i<=d; i++)
            for(int j=1; j<=d; j++)
                m[i][j]=min(m[i][j],m[i][k]+m[k][j]);
}
int main()
{
    int n;
    while(scanf("%d",&n),n!=-1)
    {
        int w,d=0;
        init();
        string a,b,a1,b1;  //d 用来记录每一个车站的编号
        cin>>a1>>b1;
        map<string,int> mp;//我们定义map来映射每个站点
        mp.clear();
        mp[a1]=++d;//这里我们设置起点为1  终点为2 就好了
        mp[b1]=++d;
        while(n--)
        {
            cin>>a>>b>>w;
            if(mp[a]==0) mp[a]=++d;
            if(mp[b]==0) mp[b]=++d;
            if(m[mp[a]][mp[b]]>w)//判断重边  也不知道数据里面存不存在 
            {
                m[mp[a]][mp[b]]=w;
                m[mp[b]][mp[a]]=w;
            }
        }
        /*dijsktra(mp[a1],d+1);*/
        floyed(d);
        if(a1==b1) cout<<0;//  没有判断起点终点 WA了一次
        else if(m[1][2]!=INF&&mp[b1])
            cout<<m[1][2];
        else cout<<-1;
        cout<<endl;
    }
}

After studying for a few days, the shortest way, the template questions can be written.
Here are some shortest questions for the basics of HDU: HDU 1596 1874 1869 2066 2544 Just learning konjac can practice hands


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326281360&siteId=291194637