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<cstring>
#include<queue>
#define maxx 0x3f3f3f3f
using namespace std;
struct node
{
    int to,w;
    int pre;
} edge[1000005][2];
int dis[1000005];
int vis[1000005];
int next[1000005][2];
int m,s1,s2,start,endd;
void addedge()
{
    int u,v,weight;
    scanf("%d%d%d",&u,&v,&weight);
    edge[s1][0].to=v;
    edge[s1][0].w=weight;
    edge[s1][0].pre=next[u][0];
    next[u][0]=s1++;
    edge[s2][1].to=u;
    edge[s2][1].w=weight;
    edge[s2][1].pre=next[v][1];
    next[v][1]=s2++;
    return;
}
void spfa(int d)
{
    for(int j=0; j<=m; j++)
    {
        vis[j]=0;
        dis[j]=maxx;
    }
    queue<int>q;
    int head;
    dis[1]=0;
    q.push(1);
    while(!q.empty())
    {
        head=q.front();
        q.pop();
        vis[head]=0;
        for(int i=next[head][d]; i!=-1; i=edge[i][d].pre)
        {
            if(dis[head]+edge[i][d].w<dis[edge[i][d].to])
            {
                dis[edge[i][d].to]=dis[head]+edge[i][d].w;
                if(!vis[edge[i][d].to])
                {
                    vis[edge[i][d].to]=1;
                    q.push(edge[i][d].to);
                }
            }
        }
    }
    return;
}
int main()
{
    int t,n,i,j;
    __int64 ans;
    scanf("%d",&t);
    while(t--)
    {
        s1=s2=0;
        ans=0;
        scanf("%d%d",&m,&n);
        for(i=0; i<=m; i++)
            next[i][0]=next[i][1]=-1;
        while(n--)
        {
            addedge();
        }
        spfa(0);
        for(i=2; i<=m; i++)
            ans=ans+dis[i];
        spfa(1);
        for(i=2; i<=m; i++)
            ans=ans+dis[i];
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-10-16 04:11:36

poj1511||hdu1535 Invitation Cards spfa的相关文章

HDU1535——Invitation Cards(最短路径:SPAF算法)

Invitation Cards DescriptionIn 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 invitatio

HDU 1535 &amp;&amp; POJ 1511 Invitation Cards (SPFA 模板 + 反向建图)

Invitation Cards HDU: Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) POJ: Time Limit: 8000 MS     Memory Limit: 262144 K       Problem Description In the age of television, not many people attend theater performa

hdu 1535 Invitation Cards(SPFA)

Invitation Cards Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total Submission(s) : 28   Accepted Submission(s) : 14 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description In the age of telev

poj1511/zoj2008 Invitation Cards(最短路模板题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds      Memory Limit: 65536 KB In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fa

hdu1535——Invitation Cards

Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2173    Accepted Submission(s): 1056 Problem Description In the age of television, not many people attend theater performances.

poj 1511 Invitation Cards spfa 邻接矩阵

题目链接: http://poj.org/problem?id=1511 题目大意: 这道题目比较难理解,我读了好长时间,最后还是在队友的帮助下理解了题意,大意就是,以一为起点,求从一到其他各点的最短回路总和. 解题思路: 解决这个题目有几个容易错的,解决了离ac就不远了^_^. 1:数据范围是1<=边数<=顶点数<=1000000,所以不能用邻接矩阵,要用邻接表,用vector实现时要动态申请内存. 2:求得是起始点到其他点的最短回路和,我们可以建两个邻接表(一个正向,一个负向邻接表)

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