zoukankan      html  css  js  c++  java
  • CSUOJ 1808 地铁

    Description

     Bobo 居住在大城市 ICPCCamp。

    ICPCCamp 有 n 个地铁站,用 1,2,…,n 编号。 m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci 号线,位于站 ai,bi 之间,往返均需要花费 ti 分钟(即从 ai 到 bi 需要 ti 分钟,从 bi 到 ai 也需要 ti 分钟)。
    众所周知,换乘线路很麻烦。如果乘坐第 i 段地铁来到地铁站 s,又乘坐第 j 段地铁离开地铁站 s,那么需要额外花费 |ci-cj | 分钟。注意,换乘只能在地铁站内进行。
    Bobo 想知道从地铁站 1 到地铁站 n 所需要花费的最小时间。

    Input

    输入包含不超过 20 组数据。
    每组数据的第一行包含两个整数 n,m (2≤n≤105,1≤m≤105).
    接下来 m 行的第 i 行包含四个整数 ai,bi,ci,ti (1≤ai,bi,ci≤n,1≤ti≤109).
    保证存在从地铁站 1 到 n 的地铁线路(不一定直达)。

    Output

    对于每组数据,输出一个整数表示要求的值。

    Sample Input

    3 3
    1 2 1 1
    2 3 2 1
    1 3 1 1
    3 3
    1 2 1 1
    2 3 2 1
    1 3 1 10
    3 2
    1 2 1 1
    2 3 1 1
    

    Sample Output

    1
    3
    2
    

    Hint

    以边建图,通过边的权值大小进行bfs不断更新

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<queue>
    using namespace std;
    #define MAXN 100010
    #define INF 0x3fffffff
    typedef long long ll;
    struct edge{
    	ll from, to, ci,w;
    };
    struct node{
    	ll x, sum;
    	int fa;//记录连接了几个点
    	friend bool operator <(node n1, node n2)
    	{
    		return n1.sum>n2.sum;
    	}
    };
    ll ans, n, m;
    vector<int>G[MAXN];
    vector<edge>edges;
    priority_queue<node>q;
    int vis[MAXN * 2];
    void addedge(ll x, ll y, ll ci, ll w)
    {
    	edge v = { x, y, ci, w };
    	edges.push_back(v);//边
    	G[x].push_back(edges.size() - 1);//点
    }
    void bfs()
    {
    	node a = { 1, 0, -1 };
    	q.push(a);
    	while (!q.empty())
    	{
    		a = q.top();
    		q.pop();
    		if (a.x == n)
    		{
    			ans = a.sum;
    			return;
    		}
    		if (a.fa != -1 && vis[a.fa])
    			continue;
    		vis[a.fa] = 1;
    		for (int i = 0; i < G[a.x].size(); i++)
    		{
    			node b = a;
    			edge v = edges[G[a.x][i]];
    			b.x = v.to;
    			b.fa = G[a.x][i];
    			b.sum += v.w;
    			if (a.fa >= 0)
    			{
    				b.sum += abs(edges[a.fa].ci - v.ci);
    			}
    			q.push(b);
    		}
    	}
    }
    int main()
    {
    	while (~scanf("%lld%lld", &n, &m))
    	{
    		for (int i = 0; i <= n; i++)
    			G[i].clear();
    		edges.clear();
    		for (int i = 1; i <= m; i++)
    		{
    			ll x, y, ci, w;
    			scanf("%lld%lld%lld%lld", &x, &y, &ci, &w);
    			addedge(x, y, ci, w);
    			addedge(y, x, ci, w);
    		}
    		ans = INF;
    		memset(vis, 0, sizeof(vis));
    		while (!q.empty())
    			q.pop();
    		bfs();
    		cout << ans<< endl;
    	}
    	return 0;
    }
    /**********************************************************************
    	Problem: 1808
    	User: leo6033
    	Language: C++
    	Result: AC
    	Time:1844 ms
    	Memory:19208 kb
    **********************************************************************/
    


  • 相关阅读:
    Linux内存管理1---内存寻址
    UML和模式应用5:细化阶段(10)---UML交互图
    AT91RM9200---电源管理控制器(PMC)介绍
    AT91RM9200---定时器简介
    [转]指针大小是由谁决定的?
    UML和模式应用5:细化阶段(9)---迈向对象设计
    UML和模式应用5:细化阶段(8)---逻辑架构和UML包图
    UML和模式应用5:细化阶段(7)---从需求到设计迭代进化
    UML和模式应用5:细化阶段(6)---操作契约
    VxWorks软件开发项目实例完全解析1-VxWorks简介
  • 原文地址:https://www.cnblogs.com/csu-lmw/p/9124435.html
Copyright © 2011-2022 走看看