http://poj.org/problem?id=1511 http://acm.hdu.edu.cn/showproblem.php?pid=1535
Invitation Cards
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1044 Accepted Submission(s): 459
Problem 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 the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery.
The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan.
All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees.
Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.
Output
For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.
Sample Input
2 2 2 1 2 13 2 1 33 4 6 1 2 10 2 1 60 1 3 20 3 4 10 2 4 5 4 1 50
Sample Output
46 210
【思路】题很简单,就是正反跑两遍dj。然而错了一晚上,最后才发现用邻接表板子极为毒瘤,HDU MLE,POJ TLE。换了正版邻接表就好了。还是要好好学邻接表啊。
【邻接表板板】
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
const ll INF=10000000000;
const int M=1000020;
class Edge
{
public:
ll v,next,w;
};
Edge edges[M];
int head[M],cnt;
ll d[M];
int n;
ll u[M],v[M],w[M];
struct HeapNode
{
ll d,u;
bool operator < (const HeapNode& rhs)const
{
return d>rhs.d;
}
};
priority_queue<HeapNode>Q;
bool done[M]= {0};
void init()
{
memset(done,0,sizeof(done));
memset(head, -1, sizeof(head));
while(!Q.empty())
Q.pop();
cnt=0;
}
void addedge(int u,int v,int w)
{
edges[cnt].v=v;
edges[cnt].w=w;
edges[cnt].next=head[u];
head[u]=cnt++;
}
int main()//DJ
{
int t;
scanf("%d",&t);
while(t)
{
t--;
init();
scanf("%d",&n);
int m;
scanf("%d",&m);
for(int i=0; i<m; i++)
{
scanf("%I64d%I64d%I64d",&u[i],&v[i],&w[i]);
addedge(u[i],v[i],w[i]);
}
int s=1;
for(int i=1; i<=n; i++)
d[i]=INF;
d[s]=0;
Q.push(HeapNode{0,s});
while(!Q.empty())
{
HeapNode x=Q.top();
Q.pop();
int u=x.u;
if(done[u])
continue;
done[u]=1;
for(int i=head[u]; i!=-1; i=edges[i].next)
{
Edge&e=edges[i];
if(d[e.v]>d[u]+e.w)
{
d[e.v]=d[u]+e.w;
Q.push(HeapNode{d[e.v],e.v});
}
}
}
}
}
【AC代码】
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
const ll INF=10000000000;
const int M=1000020;
class Edge
{
public:
ll v,next,w;
};
Edge edges[M];
int head[M],cnt;
ll d[M];
int n;
ll u[M],v[M],w[M];
struct HeapNode
{
ll d,u;
bool operator < (const HeapNode& rhs)const
{
return d>rhs.d;
}
};
priority_queue<HeapNode>Q;
bool done[M]= {0};
void init()
{
memset(done,0,sizeof(done));
memset(head, -1, sizeof(head));
while(!Q.empty())
Q.pop();
cnt=0;
}
void addedge(int u,int v,int w)
{
edges[cnt].v=v;
edges[cnt].w=w;
edges[cnt].next=head[u];
head[u]=cnt++;
}
int main()//DJ
{
int t;
scanf("%d",&t);
while(t)
{
t--;
init();
scanf("%d",&n);
int m;
scanf("%d",&m);
for(int i=0; i<m; i++)
{
scanf("%I64d%I64d%I64d",&u[i],&v[i],&w[i]);
addedge(u[i],v[i],w[i]);
}
int s=1;
for(int i=1; i<=n; i++)
d[i]=INF;
d[s]=0;
Q.push(HeapNode{0,s});
while(!Q.empty())
{
HeapNode x=Q.top();
Q.pop();
int u=x.u;
if(done[u])
continue;
done[u]=1;
for(int i=head[u]; i!=-1; i=edges[i].next)
{
Edge&e=edges[i];
if(d[e.v]>d[u]+e.w)
{
d[e.v]=d[u]+e.w;
Q.push(HeapNode{d[e.v],e.v});
}
}
}
ll ans=0;
for(int i=1; i<=n; i++)
ans+=d[i];
init();
for(int i=0; i<m; i++)
{
addedge(v[i],u[i],w[i]);
}
s=1;
for(int i=1; i<=n; i++)
d[i]=INF;
d[s]=0;
Q.push(HeapNode{0,s});
while(!Q.empty())
{
HeapNode x=Q.top();
Q.pop();
int u=x.u;
if(done[u])
continue;
done[u]=1;
for(int i=head[u]; i!=-1; i=edges[i].next)
{
Edge&e=edges[i];
if(d[e.v]>d[u]+e.w)
{
d[e.v]=d[u]+e.w;
Q.push(HeapNode{d[e.v],e.v});
}
}
}
for(int i=1; i<=n; i++)
ans+=d[i];
printf("%I64d\n",ans);
}
}
【毒瘤代码】
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
const ll INF=10000000000;
const int M=1000020;
class Edge
{
public:
ll u,v,w;
};
vector<Edge>edges;
vector<int>G[M];
ll d[M];
ll p[M];
vector<Edge>edges2;
vector<int>G2[M];
ll d2[M];
ll p2[M];
int n;
struct HeapNode
{
ll d,u;
bool operator < (const HeapNode& rhs)const
{
return d>rhs.d;
}
};
priority_queue<HeapNode>Q;
priority_queue<HeapNode>Q2;
bool done[M]= {0};
bool done2[M]= {0};
int main()//DJ
{
int t;
scanf("%d",&t);
while(t)
{
t--;
memset(done,0,sizeof(done));
memset(done2,0,sizeof(done2));
edges2.clear();edges.clear();
scanf("%d",&n);
int m;
scanf("%d",&m);
for(int i=0;i<m;i++){G[i].clear();G2[i].clear();}
for(int i=0; i<m; i++)
{
Edge e,e2;
scanf("%lld%lld%lld",&e.u,&e.v,&e.w);
edges.push_back(e);
G[e.u].push_back(edges.size()-1);
e2.u=e.v;
e2.v=e.u;
e2.w=e.w;
edges2.push_back(e2);
G2[e2.u].push_back(edges2.size()-1);
}
int s=1;
for(int i=1; i<=n; i++)
d[i]=INF;
d[s]=0;
Q.push(HeapNode{0,s});
while(!Q.empty())
{
HeapNode x=Q.top();
Q.pop();
int u=x.u;
if(done[u])
continue;
done[u]=1;
for(int i=0; i<G[u].size(); i++)
{
Edge&e=edges[G[u][i]];
if(d[e.v]>d[u]+e.w)
{
d[e.v]=d[u]+e.w;
p[e.v]=G[u][i];
Q.push(HeapNode{d[e.v],e.v});
}
}
}
s=1;
for(int i=1; i<=n; i++)
d2[i]=INF;
d2[s]=0;
Q2.push(HeapNode{0,s});
while(!Q2.empty())
{
HeapNode x=Q2.top();
Q2.pop();
int u=x.u;
if(done2[u])
continue;
done2[u]=1;
for(int i=0; i<G2[u].size(); i++)
{
Edge&e=edges2[G2[u][i]];
if(d2[e.v]>d2[u]+e.w)
{
d2[e.v]=d2[u]+e.w;
p2[e.v]=G2[u][i];
Q2.push(HeapNode{d[e.v],e.v});
}
}
}
ll ans=0;
for(int i=1; i<=n; i++)
ans+=d[i]+d2[i];
printf("%lld\n",ans);
}
}