aizu2249最短路Road Construction

题目链接

求让城市1到其他城市在保证最短路的情况下求最小花费,最短路水题。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int INF=1e9;
struct edge
{
    int u,v;
    int len,cost,next;
}e[100010<<2];
int head[100010<<2];
int vis[100010<<2];
int dis[100010<<2];
int ans[100010<<2];
int cnt;
void Initial()
{
    memset(head,-1,sizeof(head));
}
void spfa()
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=100010;i++){
        dis[i]=INF;
        ans[i]=INF;
    }
    //memset(ans,1,sizeof(ans));
    //memset(dis,1,sizeof(dis));
    queue<int> que;
    while(!que.empty()){
        que.pop();
    }
    que.push(1);
    vis[1]=1;
    dis[1]=0;
    ans[1]=0;
    while(!que.empty()){
         int d=que.front();
         que.pop();
         vis[d]=0;
         for(int i=head[d];i!=-1;i=e[i].next){
            if(dis[e[i].v]==dis[d]+e[i].len && ans[e[i].v]>e[i].cost){
                ans[e[i].v]=e[i].cost;
            }
            if(dis[e[i].v]>dis[d]+e[i].len){
               dis[e[i].v]=dis[d]+e[i].len;
               ans[e[i].v]=e[i].cost;
               if(!vis[e[i].v]){
                   que.push(e[i].v);
                   vis[e[i].v]=1;
               }
            }

         }
    }

}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)&& (n!=0 || m!=0)){
        cnt=0;
        Initial();
        for(int i=1;i<=m;i++){
            scanf("%d%d%d%d",&e[cnt].u,&e[cnt].v,&e[cnt].len,&e[cnt].cost);
            e[cnt].next=head[e[cnt].u];
            head[e[cnt].u]=cnt;
            cnt++;
            e[cnt].u=e[cnt-1].v;
            e[cnt].v=e[cnt-1].u;
            e[cnt].cost=e[cnt-1].cost,e[cnt].len=e[cnt-1].len;
            e[cnt].next=head[e[cnt].u];
            head[e[cnt].u]=cnt;
            cnt++;
        }
        spfa();
        int Cost=0;
        for(int i=2;i<=n;i++){
            Cost+=ans[i];
        }
        printf("%d\n",Cost);
    }
    return 0;
}
时间: 2024-11-08 21:04:52

aizu2249最短路Road Construction的相关文章

BZOJ 3040: 最短路(road) ( 最短路 )

本来想学一下配对堆的...结果学着学着就偏了... 之前 kpm 写过这道题 , 前面的边不理它都能 AC .. 我也懒得去写前面的加边了... 用 C++ pb_ds 库里的 pairing_heap 水过去的... ---------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #

POJ 3352 Road Construction(图论-tarjan)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8647   Accepted: 4318 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro

poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&amp;&amp;缩点】

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 5031 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the r

BZOJ 3040: 最短路(road) [Dijkstra + pb_ds]

3040: 最短路(road) Time Limit: 60 Sec  Memory Limit: 200 MBSubmit: 2476  Solved: 814[Submit][Status][Discuss] Description N个点,M条边的有向图,求点1到点N的最短路(保证存在).1<=N<=1000000,1<=M<=10000000 Input 第一行两个整数N.M,表示点数和边数.第二行六个整数T.rxa.rxc.rya.ryc.rp. 前T条边采用如下方式生成

POJ 3352 Road Construction 使得无向图边变双连通图

点击打开链接 Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8168   Accepted: 4106 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of

POJ 题目 Road Construction(双连通)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9353   Accepted: 4648 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro

POJ 3177 Redundant Paths POJ 3352 Road Construction(双连通)

POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的,一份代码能交,给定一个连通无向图,问加几条边能使得图变成一个双连通图 思路:先求双连通,缩点后,计算入度为1的个数,然后(个数 + 1) / 2 就是答案(这题由于是只有一个连通块所以可以这么搞,如果有多个,就不能这样搞了) 代码: #include <cstdio> #include <cstring> #include <algorithm&

Road Construction POJ - 3352 (边双连通分量)

Road Construction POJ - 3352 题意:一个无向图(无重边),问至少还要加多少边使得去掉任意一条边后任意两点仍可互达. 无向图的边双连通分量(无重边) 先用一次dfs标记出割边,然后dfs标记出各联通分量 再根据割边,缩点重新建图,生成一颗树 则答案就是(叶子树+1)/2. 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring

Poj 3352 Road Construction &amp; Poj 3177 Redundant Paths(边双连通分量+缩点)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9465   Accepted: 4699 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro