zoukankan      html  css  js  c++  java
  • kuangbin_ShortPath N (POJ 1847)

    模板题辣很简单的 只有两种val 0 和1

    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <set>
    #include <algorithm>
    #define INF 0x3F3F3F3F
    using namespace std;
    
    typedef pair<int, int> pii;
    struct cmp{
        bool operator () (const pii a, const pii b){
            return a.first > b.first;
        }
    };
    
    int val[110][110];
    int n, a, b;
    
    int dij()
    {
        int  dist[110];
        priority_queue<pii, vector<pii>, cmp> q;
        memset(dist, 0x3f, sizeof dist);
        dist[a] = 0;
        q.push(make_pair(0, a));
        while(!q.empty()){
            pii u = q.top();
            q.pop();
            //printf("u.second = %d
    ", u.second);
            if(u.first > dist[u.second]) continue;
            for(int i = 1; i <= n; i++){
                if(dist[i] > dist[u.second] + val[u.second][i]){
                    dist[i] = dist[u.second] + val[u.second][i];
                    q.push(make_pair(dist[i], i));
                }
            }
        }
        return dist[b] == INF ? -1 : dist[b];
    }
    
    int main()
    {
        memset(val, 0x3f, sizeof val);
        scanf("%d%d%d", &n, &a, &b);
        for(int i = 1; i <= n; i++){
            int m, to;
            scanf("%d", &m);
            if(m == 0) continue;
            m--;
            scanf("%d", &to);
            val[i][to] = 0;
            while(m--){
                scanf("%d", &to);
                val[i][to] = 1;
            }
        }
        printf("%d
    ", dij());
        return 0;
    }
  • 相关阅读:
    Django ORM 之F、Q查询与事务
    Django ORM 之基于对象、双下划线查询
    Django ORM
    Django 模板层
    Django视图层
    Django 路由控制
    Django完成常用四大功能
    Django 框架之前
    HTTP协议
    CSS-学习笔记五
  • 原文地址:https://www.cnblogs.com/quasar/p/5128907.html
Copyright © 2011-2022 走看看