poj1511

题目大意:

起点为1号点,求1到所有点最短路径及所有点到1的最短路径来回总和

思路:

因为题目规模太大,不能用二维数组来存储点与点之间的权值,而且考虑到也会超时,所以选用spfa加邻接表的方式来做这道题

1-所有其他点的最短路径很好求,而其他点到1的的最短距离可以通过把起点与终点反向从而求出的便是其他点到1的距离了

代码如下:

#include <iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxs = 1000000+5;
const __int64 INF = 0xFFFFFFFF;
__int64 dist1[maxs],dist2[maxs],ans=0;
struct Edge
{
    int e,next,weight;
}edge1[maxs],edge2[maxs];
int head1[maxs],head2[maxs];
int n,m;
void spfa(Edge edge[],__int64 dist[],int head[maxs])
{
    bool vis[maxs];
    memset(vis,false,sizeof(vis));
    for(int i=1;i<=n;i++)
        dist[i]=INF;
    dist[1]=0;
    queue<int> q;
    q.push(1);
    vis[1]=true;
    while(!q.empty())
    {
        int cur = q.front();
        q.pop();
        vis[cur]=false;
        for(int i=head[cur];i!=-1;i=edge[i].next)
        {
            if(dist[edge[i].e]>dist[cur]+edge[i].weight)
            {
                dist[edge[i].e]=dist[cur]+edge[i].weight;
                if(!vis[edge[i].e])
                {
                    q.push(edge[i].e);
                    vis[edge[i].e]=true;
                }
            }

        }
    }
}
int main()
{
    freopen("in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(head1,-1,sizeof(head1));
        memset(head2,-1,sizeof(head2));
        memset(edge1,0,sizeof(edge1));
        memset(edge2,0,sizeof(edge2));
        for(int i=1;i<=m;i++)
        {
            int a,b,w;
            scanf("%d%d%d",&a,&b,&w);
            edge1[i].e=b;
            edge1[i].weight=w;
            edge1[i].next=head1[a];
            head1[a]=i;
            edge2[i].e=a;
            edge2[i].weight=w;
            edge2[i].next=head2[b];
            head2[b]=i;
        }
        ans=0;
        spfa(edge1,dist1,head1);
        spfa(edge2,dist2,head2);
        for(int i=1;i<=n;i++)
            ans+=(dist1[i]+dist2[i]);
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-10-10 02:57:15

poj1511的相关文章

ACM/ICPC 之 最短路-SPFA+正逆邻接表(POJ1511(ZOJ2008))

求单源最短路到其余各点,然后返回源点的总最短路长,以构造邻接表的方法不同分为两种解法. POJ1511(ZOJ2008)-Invitation Cards 改变构造邻接表的方法后,分为两种解法 解法一: 1 //POJ1511-ZOJ2008 2 //Time:7766Ms Memory:99112K 3 //求从1处到各点后再返回1处的最短总路长 4 //需要构造邻接表和逆邻接表 5 //构造方法1:vector构造邻接表 6 //SPFA+邻接表 7 #include<iostream>

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> #includ

poj1511 最短路 spfa

// poj1511 最短路 spfa // // Bellman-Ford 队列优化 // // 留个spfa模板,精髓就是不断松弛,并将可能会影响 // 结果的点,如果在队列中不用加,不在就加入. #include <cstdio> #include <algorithm> #include <cstring> #include <iostream> #include <queue> typedef long long ll; using n

POJ-1511(Dijkstra+优先队列优化)

Invitation Cards POJ-1511 从这道题我还是发现了很多的问题,首先就是快速输入输出,这里的ios::---这一行必须先放在main函数第一行,也就是输入最开始的前面,否则系统疯狂报WA. 其次就是,ios的位置没有错之后又疯狂地报TLE,就是超时了,这个问题要不就是算法的复杂度,还有就是输入输出还是不够快,所以首先排除输入输出的问题,所以我把ios改成了scanf所以这题就过了. 事实证明,ios的方法还是没有scanf快,所以以后还是使用scanf. 其次就是这个算法本身

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<

POJ1511(最短路大数据处理)

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

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

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( 最短路,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