poj1511 Invitation Cards

poj1511

题意:就是求所有的节点到节点 1,然后从节点1返回的最小距离的和。

简单SPFA,和poj 3268 无多少差异。

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x3f // 一个大数
#define eps 1e-6
using namespace std;
#define maxn  1000050
#define Maxn 10000000
int n,m;
int first[maxn];
int next[maxn];
int u[Maxn];
int v[Maxn];
int w[Maxn];
int dis[maxn];
int go[maxn];
int vist[maxn];
int a[Maxn],b[Maxn],c[Maxn];
queue<int> Q;
void init()
{
    memset(vist,0,sizeof(vist));
    while(!Q.empty()) Q.pop();//一次spfa后记得清空队列
    memset(dis,INF,sizeof(dis));
    dis[1]=0;
    for(int i=1;i<=n;i++)
    first[i]=-1;
}
void spfa()
{
    Q.push(1);
    vist[1]=1;//入列记为 1
    while(!Q.empty())
    {
        int t=Q.front();
        Q.pop();
        vist[t]=0;//出列记为 0
        for(int i=first[t];i!=-1;i=next[i])
        {
            if(dis[t]+w[i]<dis[v[i]])
            {
                 dis[v[i]]=dis[t]+w[i];
                 if(!vist[v[i]])
                 {
                        Q.push(v[i]);
                        vist[v[i]]=1;
                 }

            }
        }
    }
    for(int i=1;i<=n;i++)
        go[i]+=dis[i];
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        memset(go,0,sizeof(go));
        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a[i],&b[i],&c[i]);
            u[i]=a[i]; v[i]=b[i]; w[i]=c[i];
            next[i]=first[u[i]];
            first[u[i]]=i;
        }
        spfa();
        init();
        for(int i=1;i<=m;i++)
        {
            v[i]=a[i]; u[i]=b[i]; w[i]=c[i];
            next[i]=first[u[i]];
            first[u[i]]=i;
        }
        spfa();
      long long  mm=0;
     for(int i=2;i<=n;i++)
        mm+=go[i];
     printf("%I64d\n",mm);
    }
    return 0;
}

poj1511 Invitation Cards

时间: 2024-11-06 16:36:54

poj1511 Invitation Cards的相关文章

POJ-1511 Invitation Cards( 最短路,spfa )

题目链接:http://poj.org/problem?id=1511 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They ha

POJ1511 Invitation Cards【SPFA】

Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 20229 Accepted: 6612 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to

poj1511——Invitation Cards(SPFA+邻接表)

Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all

POJ-1511 Invitation Cards (双向单源最短路)

Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all

POJ1511 Invitation Cards —— 最短路spfa

题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 29286   Accepted: 9788 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidine

POJ1511:Invitation Cards(最短路)

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 34743   Accepted: 11481 题目链接:http://poj.org/problem?id=1511 Description: In the age of television, not many people attend theater performances. Antique Comedians of Malidi

POJ1511 Invitation Cards(多源单汇最短路)

边取反,从汇点跑单源最短路即可. 1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<algorithm> 5 using namespace std; 6 #define MAXN 1111111 7 #define MAXM 1111111 8 9 inline void in(int &ret){ 10 char c; ret=0; 11 while(c=get

POJ-1511 Invitation Cards (单源最短路+逆向)

<题目链接> 题目大意: 有向图,求从起点1到每个点的最短路然后再回到起点1的最短路之和. 解题分析: 在求每个点到1点的最短路径时,如果仅仅只是遍历每个点,对它们每一个都进行一次最短路算法,那么即使是用了堆优化的dijkstra,时间复杂度也高达O(n^2 logn),而本题有1000000个点,毫无疑问,这种想法必然是不可行的,所以我们可以采用逆向思维,将图中的每一条有向边全部反向,然后以1为起点,仅做一次dijkstra,就能得到1到所有点的最短距离,即反向前的,所有点到1点的最短距离.

poj1511||hdu1535 Invitation Cards spfa

题目链接: Invitation Cards 题意: 给出一个M个点N条边的单向图 1 <= M,N <= 1000000.   给出每条边的两端和经过所需要的时间,求从第一点分别到达所有其他点(2~M)再回到第一点的最短时间 题解: 1 <= M,N <= 1000000.     邻接矩阵肯定会爆内存  所以spfa邻接表解决即可 注意好反向边的建立 代码: #include<iostream> #include<cstdio> #include<