Description
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
Output
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
46210
需要建二次图:都是单源最短路;
#include<iostream> #include<vector> #include<string> #include<queue> #include<string.h> #include<stdio.h> #include<algorithm> using namespace std; //const int inf=1e10; #define inf 0xFFFFFFFF typedef struct { int w,v,next; } node; node edge[1000010]; int a[1000010][3]; int edgehead[1000010]; long long dis[1000010]; bool vis[1000010]; int n,m;int k; void jia(int u,int v,int w) { edge[k].v=v; edge[k].w=w; edge[k].next=edgehead[u]; edgehead[u]=k++; } long long spfa() { memset(vis,0,sizeof(vis)); for(int i=2; i<=n; i++) { dis[i]=inf; } dis[1]=0; queue<int>q; q.push(1); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=false; for(int i=edgehead[u]; i; i=edge[i].next) { int v=edge[i].v; int w=edge[i].w; if(dis[v]>dis[u]+w) { dis[v]=dis[u]+w; if(!vis[v]) { q.push(v); vis[v]=true; } } } } long long ans=0; for(int i=1; i<=n; i++) ans+=dis[i]; return ans; } int main() { int test; scanf("%d",&test); while(test--) { memset(edgehead,0,sizeof(edgehead)); memset(edge,0,sizeof(edge)); k=1;scanf("%d%d",&n,&m); for(int i=1; i<=m; i++) { scanf("%d%d%d",&a[i][0],&a[i][1],&a[i][2]); jia(a[i][0],a[i][1],a[i][2]); } long long ans=spfa(); memset(edgehead,0,sizeof(edgehead)); memset(edge,0,sizeof(edge)); k=1; for(int i=1; i<=m; i++) { jia(a[i][1],a[i][0],a[i][2]); } ans+=spfa(); printf("%lld\n",ans); } }
Description
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.
snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?
Input
The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.
Output
Output one line with only the largest difference desired. The difference is guaranteed to be finite.
Sample Input
2 2 1 2 5 2 1 4Sample Output
5#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> using namespace std; const int inf=0x3f3f3f3f; int edgehead[30010]; int vis[30010]; int q[30010]; int dis[30010]; typedef struct { int w,v,next; } node; node edge[150010]; int tol; void jia(int u,int v,int w)//加边 { edge[tol].w=w; edge[tol].v=v; edge[tol].next=edgehead[u]; edgehead[u]=tol++; } void spfa(int start,int n) { int top=0; for(int i=1; i<=n; i++) { if(i==start) { q[top++]=i; vis[i]=true; dis[i]=0; } else { vis[i]=false; dis[i]=inf; } } while(top!=0) { int u=q[--top]; vis[u]=false; for(int i=edgehead[u]; i; i=edge[i].next) { int v=edge[i].v; if(dis[v]>dis[u]+edge[i].w) { dis[v]=dis[u]+edge[i].w; if(!vis[v]) { vis[v]=true; q[top++]=v; } } } } } int main() { int n,m; int a,b,c; while(scanf("%d%d",&n,&m)!=EOF) { tol=1; memset(edgehead,0,sizeof(edgehead)); while(m--) { scanf("%d%d%d",&a,&b,&c); jia(a,b,c); } spfa(1,n); printf("%d\n",dis[n]); } }
我